简体   繁体   English

HTML5 canvas:重绘整个上下文

[英]HTML5 canvas: redrawing the whole context

I have this <canvas></canvas> with a couple images & text filled/drawn into it. 我有这个<canvas></canvas> ,上面有一些图片和文字。 Now I want to apply a slide-in effect on the whole canvas by changing it's offset. 现在,我想通过更改其偏移量在整个画布上应用滑入效果

Something like redraw everything on the offset (0,-10). 像重新绘制偏移量(0,-10)上的所有内容。

I know it is possible by redrawing every element and changing their offsets. 我知道可以通过重绘每个元素并更改其偏移量来实现。 But as it have many elements drawn into the canvas, I'm trying to avoid producing an excessive code just to move every element. 但是由于画布中包含许多元素,因此我试图避免产生过多的代码来移动每个元素。 Instead, I want to move the canvas as a whole using: 相反,我想使用以下方法将画布整体移动:

context.save();
context.moveTo(-10,0);
context.translate(-10,-10);
context.restore();

If I'd change the <canvas> top & left coordinates to do so, will it be hardware accelerated? 如果要更改<canvas>顶部和左侧坐标,是否可以通过硬件加速?

Have you tried: 你有没有尝试过:

 $("canvas").animate({
       left: 0px //Just an example
 }, "slow");

Above example require jquery 上面的例子需要jQuery

from http://www.urbaninsight.com/2013/01/04/improving-html5-app-performance-gpu-accelerated-css-transitions 来自http://www.urbaninsight.com/2013/01/04/improving-html5-app-performance-gpu-accelerated-css-transitions

If I'd change the top & left coordinates to do so, will it be hardware accelerated? 如果要更改顶部和左侧坐标,是否可以通过硬件加速?

Yes, but it have to be set on CSS3: 是的,但是必须在CSS3上进行设置:

.accelerated {
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -ms-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);

  -moz-transition: all 1s;
  -webkit-transition: all 1s;
  -o-transition: all 1s;
  transition: all 1s ;
}

Well, despite the process can be done with a javascript for() to change the offsets, I think it will be a better code if I use CSS3 HW Acc. 好吧,尽管可以使用javascript for()更改偏移量来完成此过程,但我认为如果使用CSS3 HW Acc,它将是更好的代码。 instead as there are many elements to be moved at once. 相反,因为有许多元素可以一次移动。

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

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