JSFiddle - React, Tailwind, and code Playground
by m1erickson
HTML
<p>Text replaced by image</p>
<canvas id="canvas" width=300 height=150></canvas>
<br>
<p>The image</p>
<img width=300 height=150 src="http://images4.fanpop.com/image/photos/23400000/water-water-23444632-2048-1277.jpg">
CSS
body {
background-color: ivory;
}
#canvas {
border:1px solid red;
}
JavaScript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = document.createElement("img");
img.onload = function () {
draw();
}
img.src = "http://images4.fanpop.com/image/photos/23400000/water-water-23444632-2048-1277.jpg";
function draw(x) {
ctx.save();
ctx.beginPath();
// put text on canvas
ctx.font = "72pt Verdana";
ctx.fillText("XBOX", 10, 100);
ctx.fill();
// use compositing to draw the background image
// only where the text has been drawn
ctx.beginPath();
ctx.globalCompositeOperation = "source-in";
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
ctx.restore();
}