ローテーションプラグインを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 以外の角度で回転すると、イメージが端で「食い尽くされる」ため、結果は望ましくありません。
面白いことに、元に戻すと(回転した画像の明るさを複数回変更したいとしましょう)、元の画像がキャンバスに表示されますが、歪んでいます
そして 3 つ目の問題は、画像を 90 度回転すると、すべてがうまく機能し、画像が回転して元の位置 (左側) に留まることです。ただし、45 度の回転を行うと、キャンバスのサイズが再調整されず、画像が中央にとどまります。これは修正できますか?誰かがそれを正しく動作させましたか? 別のライブラリをお勧めしますか?回転機能が必要です。