简体   繁体   English

如何使用Tweenlite对画布中的图像进行动画处理

[英]How to animate image in a canvas using tweenlite

Been searching for a while now and I haven't seen any article on how to animate (loop continuously from right to left) an imaged drawn in a canvas 现在已经搜索了一段时间,我还没有看过任何有关如何对画布中绘制的图像进行动画处理(从右向左连续循环)的文章。

var context = document.getElementById('my-canvas').getContext('2d');
var cloud   = new Image();
cloud.src   = 'images/cloud.png';
cloud.onload = function () {
    context.drawImage(cloud, 0, 0)
    TweenLite.to({x:0,y:0}, 2, {x: 200, y: 200});
}

What am I getting wrong? 我怎么了?

edit: 编辑:

Like so but using tweenlite : fiddle 像这样,但使用tweenlite: 小提琴

Your first argument is wrong, it needs to be the target element/object according to their documentation : 您的第一个参数是错误的, 根据其文档 ,它必须是目标元素/对象:

TweenLite.to(target, duration, vars);

where target is an object/element, duration as a number, and vars an object. 其中target是一个对象/元素, duration是一个数字,而vars是一个对象。

Example: 例:

TweenLite.to(canvas, 2, {x: 200, y:200});

Just note that if you intend to use the canvas element as target you could might as well just animate the image directly when loaded. 只需注意,如果您打算将canvas元素用作目标,则最好在加载时直接为图像设置动画。

If you intend to move it across the canvas' bitmap without moving the canvas itself you will need to use a custom object (as I understand their docs) as target holding f.ex. 如果您打算在画布的位图上移动它而不移动画布本身,则需要使用一个自定义对象(据我了解他们的文档)作为保存f.ex的目标。 the x and y properties, and use the onUpdate callback mechanism. xy属性,并使用onUpdate回调机制。 Check their documentation for all the gory details. 检查他们的文档以获取所有详细信息。

var myObj = {
        x: 0,     // start position
        y: 0,
        image: cloud
    };

TweenLite.to(myObj, 2, {x: 200, y:200, onUpdate: drawImage});

function drawImage() {
    context.clearRect(0, 0, context.canvas.width, context.canvas.height);
    context.drawImage(myObj.image, myObj.x, myObj,y);
}

Just replace what you have inside the onload handler with the code above. 只需用上面的代码替换onload处理程序中的内容即可。

Disclaimer: highly untested.. 免责声明:高度未经测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM