2020-01-17 21:11:15 +01:00
|
|
|
/**
|
|
|
|
* CanvasRenderer class
|
|
|
|
*/
|
2019-08-20 17:59:26 +02:00
|
|
|
class CanvasRenderer {
|
2020-01-17 21:11:15 +01:00
|
|
|
/**
|
|
|
|
* Renderer for CanvasJS, defines width and height for the canvas element
|
|
|
|
* @param {*} w Width for canvas element
|
|
|
|
* @param {*} h Height for canvas element
|
|
|
|
*/
|
2019-08-20 17:59:26 +02:00
|
|
|
constructor(w, h) {
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
this.w = canvas.width = w;
|
|
|
|
this.h = canvas.height = h;
|
|
|
|
this.view = canvas;
|
|
|
|
this.ctx = canvas.getContext("2d");
|
|
|
|
this.ctx.textBaseLine = "top";
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:11:15 +01:00
|
|
|
/**
|
|
|
|
* Turns off image smoothening on the canvas element
|
|
|
|
*/
|
|
|
|
setPixelated() {
|
2019-08-20 17:59:26 +02:00
|
|
|
this.ctx['imageSmoothingEnabled'] = false; /* standard */
|
|
|
|
this.ctx['mozImageSmoothingEnabled'] = false; /* Firefox */
|
|
|
|
this.ctx['oImageSmoothingEnabled'] = false; /* Opera */
|
|
|
|
this.ctx['webkitImageSmoothingEnabled'] = false; /* Safari */
|
|
|
|
this.ctx['msImageSmoothingEnabled'] = false; /* IE */
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:11:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render all children on the canvas element
|
|
|
|
* @param {*} container Containing element of the canvas element
|
|
|
|
* @param {boolean} [clear=true] Defines if the canvas element needs to be cleared for the next render
|
|
|
|
*/
|
2019-08-20 17:59:26 +02:00
|
|
|
render(container, clear = true) {
|
|
|
|
const { ctx } = this;
|
|
|
|
function renderRec(container) {
|
|
|
|
// Render container children
|
|
|
|
container.children.forEach(child => {
|
|
|
|
if (child.visible == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
|
|
|
|
if (child.pos) {
|
|
|
|
ctx.translate(Math.round(child.pos.x), Math.round(child.pos.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.anchor) {
|
|
|
|
ctx.translate(child.anchor.x, child.anchor.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.scale) {
|
|
|
|
ctx.scale(child.scale.x, child.scale.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.rotation) {
|
|
|
|
const px = child.pivot ? child.pivot.x : 0;
|
|
|
|
const py = child.pivot ? child.pivot.y : 0;
|
|
|
|
ctx.translate(px, py);
|
|
|
|
ctx.rotate(child.rotation);
|
|
|
|
ctx.translate(-px, -py);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.text) {
|
|
|
|
const { font, fill, align } = child.style;
|
|
|
|
if (font) ctx.font = font;
|
|
|
|
if (fill) ctx.fillStyle = fill;
|
|
|
|
if (align) ctx.textAlign = align;
|
2020-01-17 21:11:15 +01:00
|
|
|
ctx.fillText(child.text, 0, 0);
|
2019-08-20 17:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (child.texture) {
|
|
|
|
const img = child.texture.img;
|
|
|
|
if (child.tileW && child.tileH) {
|
|
|
|
ctx.drawImage(
|
|
|
|
img,
|
|
|
|
child.frame.x * child.tileW,
|
|
|
|
child.frame.y * child.tileH,
|
|
|
|
child.tileW, child.tileH,
|
2020-01-17 21:11:15 +01:00
|
|
|
0, 0,
|
2019-08-20 17:59:26 +02:00
|
|
|
child.tileW, child.tileH
|
|
|
|
);
|
2019-12-31 15:33:27 +01:00
|
|
|
} else if (child.imgPos && child.width && child.height) {
|
|
|
|
ctx.drawImage(
|
|
|
|
img,
|
|
|
|
child.imgPos.x,
|
|
|
|
child.imgPos.y,
|
|
|
|
child.width, child.height,
|
2020-01-17 21:11:15 +01:00
|
|
|
0, 0,
|
2019-12-31 15:33:27 +01:00
|
|
|
child.width, child.height
|
|
|
|
);
|
2019-08-20 17:59:26 +02:00
|
|
|
} else {
|
|
|
|
ctx.drawImage(img, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:11:15 +01:00
|
|
|
// Handle children with children
|
2019-08-20 17:59:26 +02:00
|
|
|
if (child.children) {
|
|
|
|
renderRec(child);
|
|
|
|
}
|
|
|
|
ctx.restore();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (clear) {
|
2020-01-17 21:11:15 +01:00
|
|
|
ctx.clearRect(0, 0, this.w, this.h);
|
2019-08-20 17:59:26 +02:00
|
|
|
}
|
|
|
|
renderRec(container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default CanvasRenderer;
|