MCPcopy Create free account
hub / github.com/CNCKitchen/stlTexturizer / loadCustomTexture

Function loadCustomTexture

js/presetTextures.js:114–133  ·  view source on GitHub ↗
(file)

Source from the content-addressed store, hash-verified

112
113export { IMAGE_PRESETS };
114
115
116/**
117 * Build a THREE.CanvasTexture + ImageData from a user-uploaded image File.
118 */
119export 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;

Callers 2

wireEventsFunction · 0.90
_applyImportedTextureFunction · 0.90

Calls 2

fitDimensionsFunction · 0.85
makeCanvasFunction · 0.85

Tested by

no test coverage detected