캔버스의 임의 위치에서 사각형을 그립니 다.하지만 모양이 캔바스 경계를 벗어나기 때문에 모양이 잘리는 경우가 있습니다. 캔버스의 다른면에 정사각형을 그려 넣을 수있는 방법을 설명 할 수 있습니까 (asteroids에서와 비슷합니다). 내가 찾은 검색은 도움이되지 못했습니다. 어떤 도움을 주셔서 감사합니다.HTML5 Canvas : 캔버스 가장자리 주위에 도형 그리기
1
A
답변
0
나는 이것을 해결했다고 생각한다. 기본적으로이 코드는 캔바스 안에있는 사각형 만 그립니다. 캔버스를 벗어나면 어떤 경계를 벗어나 위치를 확인하고 위치를 업데이트합니다. 루프 된 변수는 사각형이 항상 캔버스를 떠날 것이기 때문에 필요합니다.
var canvas = document.getElementById('bg');
var ctx = canvas.getContext('2d');
var squares = [];
squares[squares.length] = new Square(200, 200, 20, 'red', 0.785398164);
function Square(x, y, size, colour, angle)
{
this.x = x;
this.y = y;
this.size = size;
this.colour = colour;
this.speed = 5;
this.angle = angle;
this.looped = false;
}
Square.prototype.update = function()
{
this.x += (Math.cos(this.angle) * this.speed);
this.y += (Math.sin(this.angle) * this.speed);
if (this.x < canvas.width && this.x + this.size > 0 &&
this.y < canvas.height && this.y + this.size > 0)
{
this.draw();
this.looped = false;
}
else
{
if (this.x > canvas.width && !this.looped)
{
this.x = -this.size;
}
if (this.x + this.size < 0 && !this.looped)
{
this.x = this.size + canvas.width;
}
if (this.y > canvas.height && !this.looped)
{
this.y = -this.size;
}
if (this.y + this.size < 0 && !this.looped)
{
this.y = this.size + canvas.height;
}
this.looped = true;
}
}
Square.prototype.draw = function()
{
ctx.fillStyle = this.colour;
ctx.fillRect(this.x, this.y, this.size, this.size);
}
function gameLoop()
{
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < squares.length; i++)
{
squares[i].update();
}
requestAnimationFrame(gameLoop);
}
gameLoop();
(function()
{
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x)
{
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element)
{
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function()
{
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id)
{
clearTimeout(id);
};
}());
JSFiddle을 수행하지만 requestAnimationFrame이 좋지 않습니다.
+0
이것은 정확히 내가 필요로하는 것, 감사합니다 !! –
+0
대단히 환영합니다. :-) – Ben
관련 문제
- 1. html5 캔버스 도형 채우기
- 2. 부드러운 가장자리 html5 캔버스
- 3. HTML5 캔버스의 도형 그리기 ... 비디오가있는
- 4. html5 여러 캔버스 이미지 그리기
- 5. HTML5 캔버스 그리기 역사
- 6. Html5 캔버스 그리기 문제
- 7. Html5 캔버스 다시 그리기
- 8. HTML5 Canvas - 임의의 픽셀 색상으로 도형 채우기?
- 9. OpenCV를 사용하여 카드 가장자리 주위에 윤곽선 그리기
- 10. HTML5 캔버스 그리기 나는 캔버스에 사각형을 만들려고
- 11. 슬라이더가있는 html5 캔버스 자바 스크립트 그리기 라인
- 12. Java 캔버스 가장자리 흰색
- 13. HTML5 캔버스 (그리기 (직사각형)이 계속 사라짐)
- 14. HTML5 캔버스 그리기 및 업데이트
- 15. HTML5 캔버스 배경 스캘링 그리기
- 16. 캔버스 0px 선 그리기 (html5)
- 17. 그리드의 기저에 HTML 캔버스에 도형 그리기
- 18. HTML5 캔버스 이차 곡선에 화살표 그리기
- 19. HTML5 캔버스, 그리기 후 이미지 크기 조절
- 20. HTML5 캔버스 그리기 드래그 앤 드롭
- 21. HTML5 캔버스 - 캔버스에 그리기 컨텍스트를 저장하고 나중에
- 22. HTML5 캔버스 그리기 사용자 정의 커서
- 23. 매개 변수 변경시 HTML5 캔버스 다시 그리기
- 24. 자바 스크립트로 도형 그리기
- 25. iPhone - 2D 도형 그리기
- 26. html5-canvas 슬라이더 이동시 곡선 그리기
- 27. for 루프를 사용하여 HTML5 Canvas Circle 그리기
- 28. JavaScript 및 캔버스로 도형 그리기
- 29. Html5 Canvas to Canvas Blit
- 30. C# MVC HTML5 Canvas Data
나는 자동화 된 방법이 있다고 생각하지 않습니다. 사각형을 두 번 렌더링합니다. 각면에 한 번 (네 모퉁이가 겹치는 경우 네 번). –
"toroidal 토폴로지"를 찾아보십시오 –
실제로 원하는 것이 무엇인지에 대한 자세한 내용을 추가하십시오. –