在IE中,我可以使用:
<img src="http://example.com/image.png" style="filter:FlipH">
Run Code Online (Sandbox Code Playgroud)
实现水平翻转图像.
有没有办法在HTML5中水平翻转?(也许是通过使用画布?)
谢谢所有:)
Bui*_*ted 87
canvas = document.createElement('canvas');
canvasContext = canvas.getContext('2d');
canvasContext.translate(width, 0);
canvasContext.scale(-1, 1);
this.canvasContext.drawImage(image, 0, 0);
Run Code Online (Sandbox Code Playgroud)
这是一个用于测试的精灵对象的片段,它会产生您期望的结果.
这是另一个有更多细节的网站.http://andrew.hedges.name/widgets/dev/
rob*_*rtc 68
你不需要HTML5,它可以用与IE相同的CSS来完成:
-moz-transform: scale(-1, 1);
-webkit-transform: scale(-1, 1);
-o-transform: scale(-1, 1);
transform: scale(-1, 1);
filter: FlipH;
Run Code Online (Sandbox Code Playgroud)
小智 11
我喜欢上面的Eschers功能.我让它变得更整洁更好.除了翻盖,我还添加了翻牌(垂直).还可以围绕图像的中心而不是左上方绘制/旋转.最后,该函数不需要所有参数.img,x和y是必需的,但其余的不是.
如果你使用的是context.drawImage(...)之类的东西,你现在可以使用drawImage(...)并添加这里解释的rotate/flip/flop功能:
function drawImage(img, x, y, width, height, deg, flip, flop, center) {
context.save();
if(typeof width === "undefined") width = img.width;
if(typeof height === "undefined") height = img.height;
if(typeof center === "undefined") center = false;
// Set rotation point to center of image, instead of top/left
if(center) {
x -= width/2;
y -= height/2;
}
// Set the origin to the center of the image
context.translate(x + width/2, y + height/2);
// Rotate the canvas around the origin
var rad = 2 * Math.PI - deg * Math.PI / 180;
context.rotate(rad);
// Flip/flop the canvas
if(flip) flipScale = -1; else flipScale = 1;
if(flop) flopScale = -1; else flopScale = 1;
context.scale(flipScale, flopScale);
// Draw the image
context.drawImage(img, -width/2, -height/2, width, height);
context.restore();
}
Run Code Online (Sandbox Code Playgroud)
例子:
var myCanvas = document.getElementById("myCanvas");
var context = myCanvas.getContext("2d"); // i use context instead of ctx
var img = document.getElementById("myImage"); // your img reference here!
drawImage(img, 100, 100); // just draw it
drawImage(img, 100, 100, 200, 50); // draw it with width/height specified
drawImage(img, 100, 100, 200, 50, 45); // draw it at 45 degrees
drawImage(img, 100, 100, 200, 50, 0, true); // draw it flipped
drawImage(img, 100, 100, 200, 50, 0, false, true); // draw it flopped
drawImage(img, 100, 100, 200, 50, 0, true, true); // draw it flipflopped
drawImage(img, 100, 100, 200, 50, 45, true, true, true); // draw it flipflopped and 45 degrees rotated around the center of the image :-)
Run Code Online (Sandbox Code Playgroud)
笔记。这也可以通过 CSS 来完成。
这是一个简单的实用函数,可以水平、垂直或两者镜像图像。
function mirrorImage(ctx, image, x = 0, y = 0, horizontal = false, vertical = false){
ctx.save(); // save the current canvas state
ctx.setTransform(
horizontal ? -1 : 1, 0, // set the direction of x axis
0, vertical ? -1 : 1, // set the direction of y axis
x + (horizontal ? image.width : 0), // set the x origin
y + (vertical ? image.height : 0) // set the y origin
);
ctx.drawImage(image,0,0);
ctx.restore(); // restore the state as it was when this function was called
}
Run Code Online (Sandbox Code Playgroud)
用法
mirrorImage(ctx, image, 0, 0, true, false); // horizontal mirror
mirrorImage(ctx, image, 0, 0, false, true); // vertical mirror
mirrorImage(ctx, image, 0, 0, true, true); // horizontal and vertical mirror
Run Code Online (Sandbox Code Playgroud)
很多时候你会想在图像上绘图。我喜欢称它们为可绘制图像。要使图像可绘制,请将其转换为画布
将图像转换为画布。
function makeImageDrawable(image){
if(image.complete){ // ensure the image has loaded
var dImage = document.createElement("canvas"); // create a drawable image
dImage.width = image.naturalWidth; // set the resolution
dImage.height = image.naturalHeight;
dImage.style.width = image.style.width; // set the display size
dImage.style.height = image.style.height;
dImage.ctx = dImage.getContext("2d"); // get drawing API
// and add to image
// for possible later use
dImage.ctx.drawImage(image,0,0);
return dImage;
}
throw new ReferenceError("Image is not complete.");
}
Run Code Online (Sandbox Code Playgroud)
var dImage = makeImageDrawable(image); // convert DOM img to canvas
mirrorImage(dImage.ctx, dImage, 0, 0, false, true); // vertical flip
image.replaceWith(dImage); // replace the DOM image with the flipped image
Run Code Online (Sandbox Code Playgroud)
如果您希望能够沿任意线镜像,请参阅答案沿线镜像
归档时间: |
|
查看次数: |
81725 次 |
最近记录: |