简体   繁体   English

画布html中的多个图像

[英]multiple images in canvas html

I need to display a few images which will be one on top of the other in some parallel areas. 我需要显示一些图像,这些图像将在某些平行区域中彼此重叠。 What is the right way to do it if each image should be rotated in a different angle and located in different coordinates (x,y)? 如果每个图像应该以不同的角度旋转并位于不同的坐标(x,y)中,那么正确的方法是什么?

Do I need to draw them on a single canvas and somehow rotate them differently in that canvas, or should I create different canvas for each image? 我是否需要在单个画布上绘制它们,并以某种方式在该画布中旋转它们,还是应该为每个图像创建不同的画布? once I created different canvas for each image, it displayed only the last image and the canvases were one next to the other with no parallel areas.. 一旦我为每个图像创建了不同的画布,它将仅显示最后一个图像,并且这些画布是彼此相邻的,没有平行区域。

Thanks... 谢谢...

canvas has those very useful transforms that you can use. canvas具有您可以使用的非常有用的转换。 What you need right now is canvas' .translate and .rotate 您现在需要的是.translate.rotate

Let's say you make a function which takes an img image, x coordinate, y coordinate and rad rotation to your top left corner argument, this is how you'd do it: 假设您创建了一个将img图像, x坐标, y坐标和rad旋转带到左上角参数的函数,这是您要执行的操作:

function displayRotatedImage( img, x, y, rad ){

    ctx.translate( x, y ); // ctx is your canvas context
    ctx.rotate( rad );
    ctx.drawImage( img, 0, 0 );
    ctx.rotate( -rad ); // don't forget to get everything to its previous state!
    ctx.translate( -x, -y );


}

Do you see what's going on? 你知道发生了什么吗? first of all we move to the pivot of the rotation using .translate , then we rotate around that pivot using .rotate , and we draw our image relatively to our system, and since 0 0 is our pivot point now, thanks to the translate, we simply draw the image at 0 0. Now we have to derotate and detranslate to get the canvas to it's original transform ;) 首先,我们使用移动到旋转枢轴.translate ,那么我们就绕着使用支点.rotate ,我们把我们的形象比较我们的系统,并自0 0是我们的支点现在,多亏了翻译,我们只需要在0 0处绘制图像即可。现在我们必须进行反旋转和反变换,才能将画布还原为其原始变换;)

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

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