I know how to create a perspective effect in vanilla CSS, but how can I create this effect in a canvas?
.scene {
width: 200px;
height: 200px;
border: 2px solid black;
margin: 40px;
}
.panel {
width: 100%;
height: 100%;
background: red;
/* perspective function in transform property */
transform: perspective(600px) rotateY(45deg);
}
<div class="scene">
<div class="panel"></div>
</div>
I tried the setTransform() method without sucess.
function drawScene(margin, size) {
ctx.strokeStyle = "black";
ctx.strokeRect(margin, margin, size, size);
}
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var margin = 100;
var size = 200;
drawScene(margin, size)
ctx.setTransform(1, -0.1, 0, 1, -10, 0);
//ctx.rotate(1 * Math.PI / 180); how to rotateY
ctx.fillStyle = "red";
ctx.fillRect(margin, margin, size, size);
ctx.fillStyle = 'black';
ctx.font = "48px Courier";
ctx.fillText("hello", margin, size);
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #d3d3d3;"></canvas>
I tried both solution from HTML Canvas: Rotate the Image 3D effect but none nailed it. The perspective effet isnt here.