首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CamanJS恢复和旋转修复

CamanJS恢复和旋转修复
EN

Stack Overflow用户
提问于 2014-03-20 07:54:14
回答 1查看 1.4K关注 0票数 1

如果在CamanJS中使用旋转插件,则在试图还原更改时会出现问题。只有在裁剪或调整图像大小时才能很好地实现Caman,而不是当您旋转图像时。当您恢复和图像旋转时,图像重新加载扭曲,因为它没有考虑到画布已经旋转和改变大小。另外,画布的imageData.data现在也不同了。所以我想我是通过观察他是如何实现调整尺寸的。我所做的(他也做过)基本的事情是:

  1. 创建处于初始状态的画布
  2. 从pixelData更新他的initialState
  3. 创建一个新画布
  4. 用最初的图像旋转他
  5. 获取ImageData并重新修改它们

所以我说的是。我需要知道图像旋转了多少个角度,以便在旋转新画布时得到正确的imageData (步骤4)。

代码语言:javascript
复制
this.angle=0; //added it in the constructor

我还在构造函数中添加了一个新的布尔值,以告诉我是否旋转了画布。

代码语言:javascript
复制
this.rotated = false;

在旋转插件中:

代码语言:javascript
复制
Caman.Plugin.register("rotate", function(degrees) {
    //....
    //....
    //....
    this.angle += degrees;
    this.rotated = true;
    return this.replaceCanvas(canvas);
}

在originalVisiblePixels原型上:

代码语言:javascript
复制
else if (this.rotated){
    canvas = document.createElement('canvas');//Canvas for initial state
    canvas.width = this.originalWidth; //give it the original width
    canvas.height = this.originalHeight; //and original height
    ctx = canvas.getContext('2d');
    imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    pixelData = imageData.data;//get the pixelData (length equal to those of initial canvas      
    _ref = this.originalPixelData; //use it as a reference array
    for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
        pixel = _ref[i];
        pixelData[i] = pixel; //give pixelData the initial pixels
    }
    ctx.putImageData(imageData, 0, 0); //put it back on our canvas
    rotatedCanvas = document.createElement('canvas'); //canvas to rotate from initial
    rotatedCtx = rotatedCanvas.getContext('2d');
    rotatedCanvas.width = this.canvas.width;//Our canvas was already rotated so it has been replaced. Caman's canvas attribute is allready rotated, So use that width
    rotatedCanvas.height = this.canvas.height; //the same
    x = rotatedCanvas.width / 2; //for translating
    y = rotatedCanvas.width / 2; //same
    rotatedCtx.save();
    rotatedCtx.translate(x, y);
    rotatedCtx.rotate(this.angle * Math.PI / 180); //rotation based on the total angle
    rotatedCtx.drawImage(canvas, -canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height); //put the image back on canvas
    rotatedCtx.restore(); //restore it
    pixelData = rotatedCtx.getImageData(0, 0, rotatedCanvas.width, rotatedCanvas.height).data; //get the pixelData back       
    width = rotatedCanvas.width; //used for returning the pixels in revert function               
}

您还需要在重置原型函数中添加一些重置。基本复位角和旋转

代码语言:javascript
复制
Caman.prototype.reset = function() {
    //....
    //....
    this.angle = 0;
    this.rotated = false;
}

仅此而已。

我用过它至今还在工作。你觉得呢?希望能帮上忙

EN

回答 1

Stack Overflow用户

发布于 2014-09-14 18:52:05

谢谢你,在做了一个小小的改变之后,它就起作用了。

在originalVisiblePixels原型内部的else语句中,我进行了更改:

代码语言:javascript
复制
x = rotatedCanvas.width / 2; //for translating
y = rotatedCanvas.width / 2; //same

至:

代码语言:javascript
复制
x = rotatedCanvas.width / 2; //for translating
y = rotatedCanvas.height/ 2; //same

在此之前,我的图像被切割。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22526688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档