简体   繁体   English

如何在HTML5画布中平滑地缩放图案图像?

[英]How to scale pattern image smoothly in HTML5 canvas?

I am using setTransform to scale my background pattern image. 我正在使用setTransform来缩放我的背景图案图像。 The scale starts from 1, and every time the scale decreases 0.01, until it reaches 0.5. 刻度从1开始,每次刻度减少0.01,直到达到0.5。

So here is the scaling code: 所以这是缩放代码:

context.setTransform(scale, 0, 0, scale, position.x, position.y);

In the update: 在更新中:

scale -= 0.01;

Using the above code, when I animate the changes the background pattern image is replaced with the scaled pattern image correctly. 使用上面的代码,当我为更改设置动画时,背景图案图像将被正确替换为缩放图案图像。 However the transition isn't very smooth. 然而,过渡并不是很顺利。 It works like the image suddenly flashed to another image in a short time but noticeable. 它的工作原理就像图像在短时间内突然闪现到另一个图像但很明显。 I want the replacement to be smoother and unnoticeable but I have no idea how to achieve that effect. 我希望替换更顺畅和不明显,但我不知道如何实现这种效果。

You can use translate() instead of setTransform() 你可以使用translate()而不是setTransform()

 ctx.translate(pt.x,pt.y); ctx.scale(factor,factor); ctx.translate(-pt.x,-pt.y); 

In short, you want to translate() the canvas context by your offset, scale() it to zoom in or out, and then translate() back by the opposite of the mouse offset. 简而言之,您希望通过偏移量来translate()画布上下文,使用scale()来放大或缩小,然后translate()回鼠标偏移的反面。 Note that you need to transform the cursor position from screen space into the transformed canvas context. 请注意,您需要将光标位置从屏幕空间转换为已转换的画布上下文。

Check this question for more information and live examples. 查看问题以获取更多信息和实例。

You can use setTransform 您可以使用setTransform

The first two numbers are the direction and length (scale) of the x axis, the next two are the direction and length of the y axis and the last two are the location of the origin. 前两个数字是x轴的方向和长度(比例),接下来的两个是y轴的方向和长度,后两个是原点的位置。

The quickest way to set translation, scale, and rotation 设置平移,缩放和旋转的最快方法

var scale = ?
var originX = ?
var originY = ?
var rotation = ?
ctx.setTransform(scale,0,0,scale,originX,originY);
ctx.rotate(rotation);

or you can set the rotation in the setTransform by calculating the axis directions 或者您可以通过计算轴方向在setTransform中设置旋转

var scale = ?
var originX = ?
var originY = ?
var rotation = ?
// get the x axis direction and length
var xdx = Math.cos(rotation) * scale;
var xdy = Math.sin(rotation) * scale;
// if you are not skewing then the y axis is at 90deg so x is neg y and y is x
ctx.setTransform(xdx,xdy,-xdy,xdx,originX,originY);

Then if you need to restore to the default transform 然后,如果您需要恢复到默认转换

ctx.setTransform(1,0,0,1,0,0); // restore default transform

All the values for setTransform are in pixels and set transform basically describes where and what shape the renderer will create the pixel at 0,0 setTransform的所有值都以像素为单位,set transform基本上描述了渲染器在0,0处创建像素的位置和形状

Smoothing out the pan and zoom. 平滑平移和缩放。

This answer goes into more depth and gives a working example of using the mouse to scale and zoom a image using the mouse via setTransform (no need to use save restore) It also shows how to smooth the zoom and pan Pan and Zoom 这个答案将进入更深入,让使用鼠标缩放和放大使用通过的setTransform鼠标(无需使用保存恢复)一个图像的工作示例还展示了如何平滑缩放和平移平移和缩放

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

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