Helper function to draw an image with color and additive color applied * This is slower then normal drawImage when color is applied * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} context * @param {HTMLImageElement|OffscreenCanvas} image * @param {number} sx
(context, image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight, color, additiveColor, bleed=0)
| 1203 | * @param {number} [bleed] - How many pixels to shrink the source, used to fix bleeding |
| 1204 | * @memberof Draw */ |
| 1205 | function drawImageColor(context, image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight, color, additiveColor, bleed=0) |
| 1206 | { |
| 1207 | const sx2 = bleed; |
| 1208 | const sy2 = bleed; |
| 1209 | sWidth = max(1,sWidth|0); |
| 1210 | sHeight = max(1,sHeight|0); |
| 1211 | const sWidth2 = sWidth - 2*bleed; |
| 1212 | const sHeight2 = sHeight - 2*bleed; |
| 1213 | if (!canvasColorTiles || (additiveColor ? isWhite(color.add(additiveColor)) && additiveColor.a <= 0 : isWhite(color))) |
| 1214 | { |
| 1215 | // white texture with no additive alpha, no need to tint |
| 1216 | context.globalAlpha = color.a; |
| 1217 | context.drawImage(image, sx+sx2, sy+sy2, sWidth2, sHeight2, dx, dy, dWidth, dHeight); |
| 1218 | context.globalAlpha = 1; |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | // copy to offscreen canvas |
| 1223 | workReadCanvas.width = sWidth; |
| 1224 | workReadCanvas.height = sHeight; |
| 1225 | workReadContext.drawImage(image, sx|0, sy|0, sWidth, sHeight, 0, 0, sWidth, sHeight); |
| 1226 | |
| 1227 | // tint image using offscreen work context |
| 1228 | const imageData = workReadContext.getImageData(0, 0, sWidth, sHeight); |
| 1229 | const data = imageData.data; |
| 1230 | if (additiveColor && !isBlack(additiveColor)) |
| 1231 | { |
| 1232 | // slower path with additive color |
| 1233 | const colorMultiply = [color.r, color.g, color.b, color.a]; |
| 1234 | const colorAdd = [additiveColor.r * 255, additiveColor.g * 255, additiveColor.b * 255, additiveColor.a * 255]; |
| 1235 | for (let i = 0; i < data.length; ++i) |
| 1236 | data[i] = data[i] * colorMultiply[i&3] + colorAdd[i&3] |0; |
| 1237 | workReadContext.putImageData(imageData, 0, 0); |
| 1238 | context.drawImage(workReadCanvas, sx2, sy2, sWidth2, sHeight2, dx, dy, dWidth, dHeight); |
| 1239 | } |
| 1240 | else |
| 1241 | { |
| 1242 | // faster path with no additive color |
| 1243 | for (let i = 0; i < data.length; i+=4) |
| 1244 | { |
| 1245 | data[i ] *= color.r; |
| 1246 | data[i+1] *= color.g; |
| 1247 | data[i+2] *= color.b; |
| 1248 | } |
| 1249 | workReadContext.putImageData(imageData, 0, 0); |
| 1250 | context.globalAlpha = color.a; |
| 1251 | context.drawImage(workReadCanvas, sx2, sy2, sWidth2, sHeight2, dx, dy, dWidth, dHeight); |
| 1252 | context.globalAlpha = 1; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | |
| 1258 | /** Returns true if fullscreen mode is active |