JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas" width="300" height="300"></canvas>
CSS
#canvas {
border: 1px solid black;
}
JavaScript
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
var app = {
context: document.getElementById('canvas').getContext('2d'),
width: 300,
height: 300,
imageUrl: 'https://lh3.googleusercontent.com/LFkld29brpcYI1KSRgH_tGSNnlV7__uZ3l6agGRvr42XFqLtKLrJUocjcw6n-YBmcQ=w170',
image: undefined,
renderSize: {width:50, height:50},
loadImage() {
return new Promise((resolve, reject) => {
app.image = new Image();
app.image.onload = function() {
resolve();
}
app.image.src = app.imageUrl;
});
},
clearCenter() {
this.context.clearRect(this.width/2-this.renderSize.width/2, this.height/2-this.renderSize.height/2, this.renderSize.width, this.renderSize.height);
},
degreeToRadians(degree) {
return degree * Math.PI / 180;
},
draw(x, y, degree) {
let radians = this.degreeToRadians(degree);
this.context.translate(x,y);
this.context.rotate(radians);
this.context.translate(-x,-y);
this.context.drawImage(this.image,
0,0,
170, 170, (x - this.renderSize.width/2), (y - this.renderSize.height/2), this.renderSize.width, this.renderSize.height);
this.context.translate(x, y);
this.context.rotate(-radians);
this.context.translate(-x,-y);
},
currentDegree: 0,
centralRotation(){
app.clearCenter();
app.draw(150,150, app.currentDegree);
this.currentDegree+=0.1;
requestAnimationFrame(app.centralRotation);
}
}
app.loadImage().then(() => {
app.draw(25,25, 0);
app.draw(app.width-25,25, 90);
app.draw(app.width-25,app.height-25, 180);
app.draw(25,app.height-25, 275);
...