|
|
@ -6,7 +6,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
import _DEBUG from '../config/debug'; |
|
|
|
import _DEBUG from '../config/debug'; |
|
|
|
import fastBlur from '../vendor/fastBlur'; |
|
|
|
import fastBlur from '../vendor/fastBlur'; |
|
|
|
import pushHeavyTask from './heavyQueue'; |
|
|
|
import addHeavyTask from './heavyQueue'; |
|
|
|
|
|
|
|
|
|
|
|
const RADIUS = 2; |
|
|
|
const RADIUS = 2; |
|
|
|
const ITERATIONS = 2; |
|
|
|
const ITERATIONS = 2; |
|
|
@ -16,53 +16,67 @@ const DEBUG = _DEBUG && true; |
|
|
|
function processBlur(dataUri: string, radius: number, iterations: number) { |
|
|
|
function processBlur(dataUri: string, radius: number, iterations: number) { |
|
|
|
return new Promise<string>((resolve) => { |
|
|
|
return new Promise<string>((resolve) => { |
|
|
|
const img = new Image(); |
|
|
|
const img = new Image(); |
|
|
|
|
|
|
|
|
|
|
|
const perf = performance.now(); |
|
|
|
const perf = performance.now(); |
|
|
|
if(DEBUG) { |
|
|
|
if(DEBUG) { |
|
|
|
console.log('[blur] start'); |
|
|
|
console.log('[blur] start'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
img.onload = () => { |
|
|
|
img.onload = () => { |
|
|
|
const canvas = document.createElement('canvas'); |
|
|
|
const canvas = document.createElement('canvas'); |
|
|
|
canvas.width = img.width; |
|
|
|
canvas.width = img.width; |
|
|
|
canvas.height = img.height; |
|
|
|
canvas.height = img.height; |
|
|
|
|
|
|
|
|
|
|
|
const ctx = canvas.getContext('2d')!; |
|
|
|
const ctx = canvas.getContext('2d')!; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//ctx.filter = 'blur(2px)';
|
|
|
|
ctx.drawImage(img, 0, 0); |
|
|
|
ctx.drawImage(img, 0, 0); |
|
|
|
fastBlur(ctx, 0, 0, canvas.width, canvas.height, radius, iterations); |
|
|
|
fastBlur(ctx, 0, 0, canvas.width, canvas.height, radius, iterations); |
|
|
|
|
|
|
|
|
|
|
|
//resolve(canvas.toDataURL());
|
|
|
|
resolve(canvas.toDataURL()); |
|
|
|
canvas.toBlob(blob => { |
|
|
|
if(DEBUG) { |
|
|
|
|
|
|
|
console.log(`[blur] end, radius: ${radius}, iterations: ${iterations}, time: ${performance.now() - perf}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* canvas.toBlob(blob => { |
|
|
|
resolve(URL.createObjectURL(blob)); |
|
|
|
resolve(URL.createObjectURL(blob)); |
|
|
|
|
|
|
|
|
|
|
|
if(DEBUG) { |
|
|
|
if(DEBUG) { |
|
|
|
console.log(`[blur] end, radius: ${radius}, iterations: ${iterations}, time: ${performance.now() - perf}`); |
|
|
|
console.log(`[blur] end, radius: ${radius}, iterations: ${iterations}, time: ${performance.now() - perf}`); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); */ |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
img.src = dataUri; |
|
|
|
img.src = dataUri; |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const blurPromises: {[dataUri: string]: Promise<string>} = {}; |
|
|
|
const blurPromises: Map<string, Promise<string>> = new Map(); |
|
|
|
|
|
|
|
const CACHE_SIZE = 1000; |
|
|
|
|
|
|
|
|
|
|
|
export default function blur(dataUri: string, radius: number = RADIUS, iterations: number = ITERATIONS) { |
|
|
|
export default function blur(dataUri: string, radius: number = RADIUS, iterations: number = ITERATIONS) { |
|
|
|
if(!dataUri) { |
|
|
|
if(!dataUri) { |
|
|
|
console.error('no dataUri for blur', dataUri); |
|
|
|
console.error('no dataUri for blur', dataUri); |
|
|
|
return Promise.resolve(dataUri); |
|
|
|
return Promise.resolve(dataUri); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(blurPromises.size > CACHE_SIZE) { |
|
|
|
|
|
|
|
blurPromises.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(blurPromises[dataUri]) return blurPromises[dataUri]; |
|
|
|
if(blurPromises.has(dataUri)) return blurPromises.get(dataUri); |
|
|
|
return blurPromises[dataUri] = new Promise<string>((resolve) => { |
|
|
|
const promise = new Promise<string>((resolve) => { |
|
|
|
//return resolve(dataUri);
|
|
|
|
//return resolve(dataUri);
|
|
|
|
pushHeavyTask({ |
|
|
|
addHeavyTask({ |
|
|
|
items: [[dataUri, radius, iterations]], |
|
|
|
items: [[dataUri, radius, iterations]], |
|
|
|
context: null, |
|
|
|
context: null, |
|
|
|
process: processBlur |
|
|
|
process: processBlur |
|
|
|
}).then(results => { |
|
|
|
}, 'unshift').then(results => { |
|
|
|
resolve(results[0]); |
|
|
|
resolve(results[0]); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blurPromises.set(dataUri, promise); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return promise; |
|
|
|
} |
|
|
|
} |
|
|
|