【发布时间】:2015-02-18 12:29:30
【问题描述】:
我需要对这个动画做些什么才能使文本与背景图像一起动画?
我在网上看到了几个不同的示例,但要么他们没有像我那样旋转文本(这是导致问题的原因),要么他们没有解释解决方案背后的数学原理。
这很好 - http://tech.pro/tutorial/1008/creating-a-roulette-wheel-using-html5-canvas 但它不能很好地了解任何数学函数的使用。
我显然需要影响线路:
context.rotate(i * arc);
在编写文本的循环中,但我不确定所涉及的数学。
var cvs = document.getElementById("cvs");
var context = cvs.getContext("2d");
var height = 400,
width = 400,
spinning = false,
angle = 0,
awards = [100,200,300,400,500,600,700,800,900,1000,1100,1200],
segmentCount = 12,
angleAmount = 30,
arc = Math.PI / 6,
image = new Image();
image.src = 'http://placehold.it/400x400';
function draw() {
// clear
context.clearRect(0,0,width,height);
// rotate whole wheel here?
context.save();
context.translate(height/2, width/2);
context.rotate(angle * (Math.PI / 180));
context.drawImage(image,-width/2,-height/2);
context.restore();
// draw the prize amount text
for(var i = 0; i < segmentCount; i++){
context.save();
context.translate(height/2, width/2);
context.font = "bold 18px sans-serif";
context.textAlign = "end";
context.rotate(i * arc);
context.fillStyle = "#000000";
context.textBaseline = 'middle';
context.fillText(awards[i],145,0);
context.restore();
angle += angleAmount;
}
}
function update(){
draw();
angle += 5;
setTimeout(update,1000/30);
}
image.onload = update;
【问题讨论】:
-
谢谢,但不完全是,我希望背景图像旋转,文本值也像这样旋转 - tpstatic.com/_sotc/sites/default/files/1010/source/…
-
所以更像这个:jsfiddle.net/gamealchemist/fwter56k/3?我只是将当前主角度添加到每个段的旋转中。
-
啊,太好了,我试过了,但忽略了将度数转换为弧度。如果您回答此评论,我将标记为正确。
标签: javascript html animation canvas