Automatically determine whether to silhouette in challenger

This commit is contained in:
Dragon Fire
2020-03-23 12:52:24 -04:00
parent 8e6d2d2d8d
commit 0b58de7c7a
3 changed files with 20 additions and 3 deletions
+16
View File
@@ -1,3 +1,5 @@
const { createCanvas } = require('canvas');
module.exports = class CanvasUtil {
static greyscale(ctx, x, y, width, height) {
const data = ctx.getImageData(x, y, width, height);
@@ -77,6 +79,20 @@ module.exports = class CanvasUtil {
return ctx;
}
static hasAlpha(image) {
const canvas = createCanvas(image.width, image.height);
const ctx = canvas.getContext('2d');
const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
let hasAlphaPixels = false;
for (let i = 3, n = data.length; i < n; i += 4) {
if (data[i] < 255) {
hasAlphaPixels = true;
break;
}
}
return hasAlphaPixels;
}
static drawImageWithTint(ctx, image, color, x, y, width, height) {
const { fillStyle, globalAlpha } = ctx;
ctx.fillStyle = color;