This repository has been archived on 2021-02-20. You can view files and clone it, but cannot push or open issues or pull requests.
shapez.io/src/js/core/draw_utils.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-08-06 11:28:28 +02:00
/**
* @typedef {import("./sprites").AtlasSprite} AtlasSprite
* @typedef {import("./draw_parameters").DrawParameters} DrawParameters
*/
2020-05-09 16:45:23 +02:00
export function initDrawUtils() {
CanvasRenderingContext2D.prototype.beginRoundedRect = function (x, y, w, h, r) {
2020-08-06 11:28:28 +02:00
this.beginPath();
2020-05-09 16:45:23 +02:00
if (r < 0.05) {
this.rect(x, y, w, h);
return;
}
if (w < 2 * r) {
r = w / 2;
}
2020-08-06 11:28:28 +02:00
2020-05-09 16:45:23 +02:00
if (h < 2 * r) {
r = h / 2;
}
2020-08-06 11:28:28 +02:00
2020-05-09 16:45:23 +02:00
this.moveTo(x + r, y);
this.arcTo(x + w, y, x + w, y + h, r);
this.arcTo(x + w, y + h, x, y + h, r);
this.arcTo(x, y + h, x, y, r);
this.arcTo(x, y, x + w, y, r);
};
CanvasRenderingContext2D.prototype.beginCircle = function (x, y, r) {
2020-08-06 11:28:28 +02:00
this.beginPath();
2020-05-09 16:45:23 +02:00
if (r < 0.05) {
this.rect(x, y, 1, 1);
return;
}
2020-08-06 11:28:28 +02:00
this.arc(x, y, r, 0, 2.0 * Math.PI);
2020-05-09 16:45:23 +02:00
};
}
/**
*
* @param {object} param0
* @param {DrawParameters} param0.parameters
* @param {AtlasSprite} param0.sprite
* @param {number} param0.x
* @param {number} param0.y
* @param {number} param0.angle
* @param {number} param0.size
* @param {number=} param0.offsetX
* @param {number=} param0.offsetY
*/
export function drawRotatedSprite({ parameters, sprite, x, y, angle, size, offsetX = 0, offsetY = 0 }) {
parameters.context.translate(x, y);
parameters.context.rotate(angle);
sprite.drawCachedCentered(parameters, offsetX, offsetY, size, false);
parameters.context.rotate(-angle);
parameters.context.translate(-x, -y);
}