Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flip images horizontally with HTML5

Tags:

html

canvas

In IE, I can use:

<img src="http://example.com/image.png" style="filter:FlipH"> 

to implement an image flip horizontally.

Is there any way to flip horizontally in HTML5? (maybe by using canvas?)

thanks all :)

like image 427
Koerr Avatar asked Jun 27 '10 22:06

Koerr


People also ask

How do you flip an image in HTML?

Syntax: transform: rotate(90deg);

How do you flip horizontal in CSS?

You may do a transform: rotate(180deg); for the horizontal+vertical flip.


2 Answers

canvas = document.createElement('canvas'); canvasContext = canvas.getContext('2d');  canvasContext.translate(width, 0); canvasContext.scale(-1, 1); canvasContext.drawImage(image, 0, 0); 

Here's a snippet from a sprite object being used for testing and it produces the results you seem to expect.

Here's another site with more details. http://andrew.hedges.name/widgets/dev/

like image 195
Buildstarted Avatar answered Sep 23 '22 02:09

Buildstarted


You don't need HTML5, it can be done with CSS same as in IE:

-moz-transform: scale(-1, 1); -webkit-transform: scale(-1, 1); -o-transform: scale(-1, 1); transform: scale(-1, 1); filter: FlipH; 
like image 27
robertc Avatar answered Sep 23 '22 02:09

robertc