import { create as createCanvas } from './pool.js'; /** * Creates a new HTML canvas element. * @param {object} parent - The parent element to append the canvas to. * @param {number} width - The width of the canvas. * @param {number} height - The height of the canvas. * @param {string} id - The ID to assign to the canvas. * @param {boolean} skipPool - Whether to skip using the canvas pool. * @returns {HTMLCanvasElement} The created HTML canvas element. */ export const create = ( parent: object | null, width: number, height: number, id: string, skipPool: boolean ): HTMLCanvasElement => { const canvasWidth = width || 256; const canvasHeight = height || 256; const canvas = skipPool ? document.createElement('canvas') : createCanvas(parent, canvasWidth, canvasHeight); if (id !== '') { canvas.id = id; } canvas.width = canvasWidth; canvas.height = canvasHeight; canvas.style.display = 'block'; return canvas; }; /** * Sets the background color of a canvas element. * @param {HTMLCanvasElement} canvas - The canvas element to modify. * @param {string} color - The background color to set. * @returns {HTMLCanvasElement} The modified canvas element. */ export const setBackgroundColor = (canvas: HTMLCanvasElement, color = 'rgb(0,0,0)'): HTMLCanvasElement => { canvas.style.backgroundColor = color; return canvas; }; /** * Sets the touch action property of a canvas element. * @param {HTMLCanvasElement} canvas - The canvas element to modify. * @param {string} value - The touch action value to set. * @returns {HTMLCanvasElement} The modified canvas element. */ export const setTouchAction = (canvas: HTMLCanvasElement, value = 'none'): HTMLCanvasElement => { const touchAction = value || 'none'; canvas.style.setProperty('-ms-touch-action', touchAction); canvas.style.setProperty('touch-action', touchAction); return canvas; }; /** * Sets the user select property of a canvas element. * @param {HTMLCanvasElement} canvas - The canvas element to modify. * @param {string} value - The user select value to set. * @returns {HTMLCanvasElement} The modified canvas element. */ export const setUserSelect = (canvas: HTMLCanvasElement, value = 'none'): HTMLCanvasElement => { const userSelect = value || 'none'; canvas.style.setProperty('-webkit-touch-callout', userSelect); canvas.style.setProperty('-webkit-user-select', userSelect); canvas.style.setProperty('-khtml-user-select', userSelect); canvas.style.setProperty('-moz-user-select', userSelect); canvas.style.setProperty('-ms-user-select', userSelect); canvas.style.setProperty('user-select', userSelect); canvas.style.setProperty('-webkit-tap-highlight-color', 'rgba(0, 0, 0, 0)'); return canvas; }; /** * Adds a canvas element to the DOM. * @param {HTMLCanvasElement} canvas - The canvas element to add. * @param {object} parent - The parent element to append the canvas to. * @param {boolean} overflowHidden - Whether to set overflow hidden on the parent. * @returns {HTMLCanvasElement} The added canvas element. */ export const addToDOM = ( canvas: HTMLCanvasElement, parent: string | HTMLElement | null, overflowHidden = true ): HTMLCanvasElement => { let target: HTMLElement | null = null; if (typeof parent === 'string' && parent !== '') { // hopefully an element ID target = document.querySelector(`#${CSS.escape(parent)}`); } else if (typeof parent === 'object' && parent?.nodeType === 1) { target = parent; } // Fallback, covers an invalid ID and a non HTMLelement object target ??= document.body; if (overflowHidden) { target.style.overflow = 'hidden'; } target.append(canvas); return canvas; }; /** * Removes a canvas element from the DOM. * @param {HTMLCanvasElement} canvas - The canvas element to remove. */ export const removeFromDOM = (canvas: HTMLCanvasElement): void => { canvas.remove(); }; /** * Sets the transformation matrix on a canvas rendering context. * @param {CanvasRenderingContext2D} context - The canvas rendering context. * @param {number} translateX - The X translation value. * @param {number} translateY - The Y translation value. * @param {number} scaleX - The X scale value. * @param {number} scaleY - The Y scale value. * @param {number} skewX - The X skew value. * @param {number} skewY - The Y skew value. * @returns {CanvasRenderingContext2D} The modified rendering context. */ export const setTransform = ( context: CanvasRenderingContext2D, translateX: number, translateY: number, scaleX: number, scaleY: number, skewX: number, skewY: number ): CanvasRenderingContext2D => { context.setTransform(scaleX, skewX, skewY, scaleY, translateX, translateY); return context; }; /** * Gets the smoothing prefix for a canvas rendering context. * @param {CanvasRenderingContext2D} context - The canvas rendering context. * @returns {string | null} The smoothing property name or null if not found. */ export const getSmoothingPrefix = (context: CanvasRenderingContext2D): string | null => { const VENDORS = ['i', 'webkitI', 'msI', 'mozI', 'oI']; // the vendor-prefixed smoothing flags predate the standard property and are absent from lib.dom const smoothingFlags = context as unknown as Record; for (const vendor of VENDORS) { const flag = `${vendor}mageSmoothingEnabled`; if (smoothingFlags[flag] !== undefined) { return flag; } } return null; }; /** * Writes one of the vendor-prefixed image smoothing properties, which are not on the DOM type. * @param {CanvasRenderingContext2D} context - The canvas rendering context. * @param {string} property - The smoothing property name to write. * @param {boolean} value - The smoothing value to set. */ export const setSmoothing = (context: CanvasRenderingContext2D, property: string, value: boolean): void => { (context as unknown as Record)[property] = value; }; /** * Sets the smoothing enabled property on a canvas rendering context. * @param {CanvasRenderingContext2D} context - The canvas rendering context. * @param {boolean} value - The smoothing enabled value to set. * @returns {CanvasRenderingContext2D} The modified rendering context. */ export const setSmoothingEnabled = (context: CanvasRenderingContext2D, value: boolean): CanvasRenderingContext2D => { const s = getSmoothingPrefix(context); if (s !== null) { setSmoothing(context, s, value); } return context; };