有没有人让旋转插件与 camanjs 配合得很好?我已经使用 cofee 编译了 camanjs 并包含了额外的插件。其中之一是旋转。旋转插件如下
Caman.Plugin.register("rotate", function(degrees) {
var angle, canvas, ctx, height, to_radians, width, x, y;
angle = degrees % 360;
if (angle === 0) {
return this.dimensions = {
width: this.canvas.width,
height: this.canvas.height
};
}
to_radians = Math.PI / 180;
if (typeof exports !== "undefined" && exports !== null) {
canvas = new Canvas();
} else {
canvas = document.createElement('canvas');
Util.copyAttributes(this.canvas, canvas);
}
if (angle === 90 || angle === -270 || angle === 270 || angle === -90) {
width = this.canvas.height;
height = this.canvas.width;
x = width / 2;
y = height / 2;
} else if (angle === 180) {
width = this.canvas.width;
height = this.canvas.height;
x = width / 2;
y = height / 2;
} else {
width = Math.sqrt(Math.pow(this.originalWidth, 2) + Math.pow(this.originalHeight, 2));
height = width;
x = this.canvas.height / 2;
y = this.canvas.width / 2;
}
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext('2d');
ctx.save();
ctx.translate(x, y);
ctx.rotate(angle * to_radians);
ctx.drawImage(this.canvas, -this.canvas.width / 2, -this.canvas.height / 2, this.canvas.width, this.canvas.height);
ctx.restore();
return this.replaceCanvas(canvas);
});
加
Caman.Filter.register("rotate", function() {
return this.processPlugin("rotate", Array.prototype.slice.call(arguments, 0));
});
html
<img id="myimage" src="image.src">
javascript
caman = Caman("#myimage");
caman.rotate(45);
caman.render();
但是当以 90、270 -90 或 180 -180 以外的度数旋转时,结果是不想要的,因为图像在边缘被“吃掉”
有趣的是,当点击还原时(假设我想多次更改旋转图像的亮度)然后原始图像出现在画布上但扭曲了
第三个问题是,如果将图像旋转 90 度,一切正常,图像会旋转并保持在原来的位置(左侧)。但是,如果您旋转 45 度,画布不会重新调整为大小,并且图像会停留在中间。这可以修复吗?有没有人让它正常工作?你会建议另一个图书馆吗?我需要旋转功能。