HTML5 Canvas:如何通过以度数旋转图像来制作加载旋转器?
我正在用 html5 canvas 制作一个加载旋转器。我的图形在画布上,但是当我旋转它时,图像会从画布上旋转出来。我如何告诉它以中心点旋转图形?
<!DOCTYPE html>
<html>
<head>
<title>Canvas test</title>
<script type="text/javascript">
window.onload = function() {
var drawingCanvas = document.getElementById('myDrawing');
// Check the element is in the DOM and the browser supports canvas
if(drawingCanvas && drawingCanvas.getContext) {
// Initaliase a 2-dimensional drawing context
var context = drawingCanvas.getContext('2d');
//Load the image object in JS, then apply to canvas onload
var myImage = new Image();
myImage.onload = function() {
context.drawImage(myImage, 0, 0, 27, 27);
}
myImage.src = "img/loading.png";
context.rotate(45);
}
}
</script>
</head>
<body>
<canvas id="myDrawing" width="27" height="27">
</canvas>
</body>
</html>
I am making a loading spinner with html5 canvas. I have my graphic on the canvas but when i rotate it the image rotates off the canvas. How do I tell it to spin the graphic on its center point?
<!DOCTYPE html>
<html>
<head>
<title>Canvas test</title>
<script type="text/javascript">
window.onload = function() {
var drawingCanvas = document.getElementById('myDrawing');
// Check the element is in the DOM and the browser supports canvas
if(drawingCanvas && drawingCanvas.getContext) {
// Initaliase a 2-dimensional drawing context
var context = drawingCanvas.getContext('2d');
//Load the image object in JS, then apply to canvas onload
var myImage = new Image();
myImage.onload = function() {
context.drawImage(myImage, 0, 0, 27, 27);
}
myImage.src = "img/loading.png";
context.rotate(45);
}
}
</script>
</head>
<body>
<canvas id="myDrawing" width="27" height="27">
</canvas>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是完整的工作示例:)
Here is the complete working example:)
rotate
将画布(?)围绕当前位置旋转,从 0, 0 开始。您需要“移动”到所需的中心点,您可以在移动参考点后完成此操作,您希望将图像集中在该点上。 您可以这样做
您可以通过调用此命令告诉浏览器通过图像的大小开始从当前参考点的上方和左侧绘制图像,而在您从参考点开始并完全在下方绘制之前, 右(相对于画布旋转的所有方向)。
由于画布的大小就是图像的大小,因此对
translate
的调用将使用相同的测量值 (27/2) 作为 x 和 y 坐标。因此,将它们放在一起
编辑:此外,旋转单位是弧度,因此您需要在代码中将度数转换为弧度。
编辑以重新排列内容。
rotate
turns the canvas(?) around your current position, which is 0, 0 to start. you need to "move" to your desired center point, which you can accomplish withafter you move your reference point, you want to center your image over that point. you can do this by calling
this tells the browser to start drawing the image from above and to the left of your current reference point, by have the size of the image, whereas before you were starting at your reference point and drawing entirely below and to the right (all directions relative to the rotation of the canvas).
since your canvas is the size of your image, your call to
translate
will use the same measurement, (27/2), for x and y coordinates.so, to put it all together
edit: also, rotation units are radians, so you'll need to translate degrees to radians in your code.
edits for rearranging stuff.
对于其他正在研究类似问题的人,您可能想看看这个脚本,它完全符合最初的要求:
http://projects.nickstakenburg.com/spinners/
您可以在此处找到 github 源代码:
https://github.com/staaky/spinners
他使用旋转,同时保留矩形缓存,慢慢地他们年纪越大,就越淡出。
For anyone else looking into something like this, you might want to look at this script which does exactly what was originally being requested:
http://projects.nickstakenburg.com/spinners/
You can find the github source here:
https://github.com/staaky/spinners
He uses rotate, while keeping a cache of rectangles which slowly fade out, the older they are.
我找到了另一种方法来加载 html 微调器。您可以使用精灵表动画。这种方法可以通过 html5 canvas 或普通的 html/javascript/css 工作。这是一个通过html/javascript/css实现的简单方法。
它使用精灵表图像作为背景。它创建一个 Javascript 计时器来更改背景图像位置以控制精灵表动画。示例代码如下。您还可以在此处查看结果: http://jmsliu.com/1769/html -ajax-loading-spinner.html
I find another way to do html loading spinner. You can use sprite sheet animation. This approach can work both by html5 canvas or normal html/javascript/css. Here is a simple way implemented by html/javascript/css.
It uses sprite sheet image as background. It create a Javascript timer to change the background image position to control the sprite sheet animation. The example code is below. You can also check the result here: http://jmsliu.com/1769/html-ajax-loading-spinner.html