简体   繁体   English

HTML5 Canvas图像移动像素化

[英]HTML5 Canvas Image Move pixelization

I am trying to create an online image editor using HTML5 and getting issue while perform image move. 我正在尝试使用HTML5创建在线图像编辑器,并且在执行图像移动时出现问题。 pixelization happens everytime when I try to move, zoom, drag, rotate. 每当我尝试移动,缩放,拖动和旋转时,像素化都会发生。 I think issue is clearRect function. 我认为问题是clearRect函数。

function clear() {
    element.clearRect(0, 0, window.innerWidth, window.innerWidth);
}

Here is my Fiddle Project 这是我的小提琴项目

移动时的像素化

You are playing with the matrix transformations of the canvas' context. 您正在使用画布上下文的矩阵转换。
These also apply to the clearRect method. 这些也适用于clearRect方法。

You need either to calculate the current transformations and apply their negative value to the clearRect method, or to reset the transformation matrix. 您需要计算当前转换并将其负值应用于clearRect方法,或者重置转换矩阵。

One easy way to do so is by using the setTransform method and the default values for the transformation matrix : ctx.setTransform(1,0,0,1,0,0); 一种简单的方法是使用setTransform方法和转换矩阵的默认值: ctx.setTransform(1,0,0,1,0,0);

Updated fiddle , with a quick fix. 更新了小提琴 ,并提供快速修复。

very interesting topic. 非常有趣的话题。 Nice exercise, too. 锻炼也不错。 I picked your fiddle. 我选了你的小提琴。

Here is the result: 结果如下:

https://jsfiddle.net/dh6y18yh/8/ https://jsfiddle.net/dh6y18yh/8/

var loopRunner = function() {
      drawImage();
        globalID = requestAnimationFrame(loopRunner);
};

Basically the main big difference is, that I decoupled ALL the parameters for the image positioning into variables. 基本上,主要的不同是,我将图像定位的所有参数解耦为变量。 Then do the translate(x,y) in drawImage() and let drawImage() be run in a window.requestAnimationFrame() loop. 然后在drawImage()中执行translate(x,y)并让drawImage()在window.requestAnimationFrame()循环中运行。

This way in is rendered correctly at 60 fps. 这种方式可以60 fps正确渲染。

The mouse dragging is now calculating the distance dragged, and set this value to the position values. 鼠标拖动现在正在计算拖动的距离,并将此值设置为位置值。

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

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