puzzle/game/Graphics.js

38 lines
1,012 B
JavaScript
Raw Normal View History

2020-11-09 00:29:07 +01:00
function createCanvas(width = 0, height = 0) {
const c = document.createElement('canvas')
c.width = width === 0 ? window.innerWidth : width
c.height = height === 0 ? window.innerHeight : height
return c
}
async function loadImageToBitmap(imagePath) {
const img = new Image()
await new Promise((resolve) => {
img.onload = resolve
img.src = imagePath
});
2020-11-10 18:48:16 +01:00
return await createImageBitmap(img, 0, 0, img.width, img.height)
2020-11-09 00:29:07 +01:00
}
2020-11-10 18:48:16 +01:00
async function resizeBitmap (bitmap, width, height) {
const c = createCanvas(width, height)
const ctx = c.getContext('2d')
ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, width, height)
return await createImageBitmap(c)
2020-11-09 00:29:07 +01:00
}
2020-11-10 18:48:16 +01:00
async function createBitmap(width, height, color) {
const c = createCanvas(width, height)
const ctx = c.getContext('2d')
ctx.fillStyle = color
ctx.fillRect(0, 0, width, height)
return await createImageBitmap(c)
2020-11-09 20:30:37 +01:00
}
2020-11-09 00:29:07 +01:00
export default {
2020-11-10 18:48:16 +01:00
createBitmap,
2020-11-09 00:29:07 +01:00
createCanvas,
loadImageToBitmap,
resizeBitmap,
}