(file)
| 112 | |
| 113 | export { IMAGE_PRESETS }; |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Build a THREE.CanvasTexture + ImageData from a user-uploaded image File. |
| 118 | */ |
| 119 | export function loadCustomTexture(file) { |
| 120 | return new Promise((resolve, reject) => { |
| 121 | const img = new Image(); |
| 122 | const url = URL.createObjectURL(file); |
| 123 | img.onload = () => { |
| 124 | URL.revokeObjectURL(url); |
| 125 | const { w, h } = fitDimensions(img.width, img.height); |
| 126 | const canvas = makeCanvas(w, h); |
| 127 | const ctx = canvas.getContext('2d'); |
| 128 | ctx.drawImage(img, 0, 0, w, h); |
| 129 | const imageData = ctx.getImageData(0, 0, w, h); |
| 130 | const texture = new THREE.CanvasTexture(canvas); |
| 131 | texture.wrapS = texture.wrapT = THREE.RepeatWrapping; |
| 132 | texture.name = file.name; |
| 133 | resolve({ name: file.name, fullCanvas: canvas, texture, imageData, width: w, height: h }); |
| 134 | }; |
| 135 | img.onerror = () => { URL.revokeObjectURL(url); reject(new Error('Failed to load image')); }; |
| 136 | img.src = url; |
no test coverage detected