EN
JavaScript - flip canvas horizontally
0
points
In this article, we would like to show you how to flip canvas horizontally using JavaScript.
Quick solution:
var canvas = document.querySelector('#my-canvas');
var context = canvas.getContext('2d');
context.scale(-1, 1); // <--- flip horizontally
context.font = '55px Arial';
context.fillText('text', -100, 50);
Note:
The x-coordinates of all points inside the
canvasare negative now.
Practical example
In this example, we use scale() method with negative x coordinate to flip the text on canvas horizontally.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<head>
<style>
#my-canvas { border: 1px solid gray; }
</style>
</head>
<body>
<canvas id="my-canvas" width="110" height="65"></canvas>
<script>
var canvas = document.querySelector('#my-canvas');
var context = canvas.getContext('2d');
context.scale(-1, 1); // <--- flip horizontally
context.font = '55px Arial';
context.fillText('text', -100, 50);
</script>
</body>
</html>