|
|
@ -6,14 +6,14 @@ |
|
|
|
|
|
|
|
|
|
|
|
import type fastBlur from '../vendor/fastBlur'; |
|
|
|
import type fastBlur from '../vendor/fastBlur'; |
|
|
|
import addHeavyTask from './heavyQueue'; |
|
|
|
import addHeavyTask from './heavyQueue'; |
|
|
|
|
|
|
|
import IS_CANVAS_FILTER_SUPPORTED from '../environment/canvasFilterSupport'; |
|
|
|
|
|
|
|
|
|
|
|
const RADIUS = 2; |
|
|
|
const RADIUS = 2; |
|
|
|
const ITERATIONS = 2; |
|
|
|
const ITERATIONS = 2; |
|
|
|
|
|
|
|
|
|
|
|
const isFilterAvailable = 'filter' in (document.createElement('canvas').getContext('2d') || {}); |
|
|
|
|
|
|
|
let requireBlurPromise: Promise<any>; |
|
|
|
let requireBlurPromise: Promise<any>; |
|
|
|
let fastBlurFunc: typeof fastBlur; |
|
|
|
let fastBlurFunc: typeof fastBlur; |
|
|
|
if(!isFilterAvailable) { |
|
|
|
if(!IS_CANVAS_FILTER_SUPPORTED) { |
|
|
|
requireBlurPromise = import('../vendor/fastBlur').then(m => { |
|
|
|
requireBlurPromise = import('../vendor/fastBlur').then(m => { |
|
|
|
fastBlurFunc = m.default; |
|
|
|
fastBlurFunc = m.default; |
|
|
|
}); |
|
|
|
}); |
|
|
@ -21,14 +21,17 @@ if(!isFilterAvailable) { |
|
|
|
requireBlurPromise = Promise.resolve(); |
|
|
|
requireBlurPromise = Promise.resolve(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function processBlurNext(img: HTMLImageElement, radius: number, iterations: number) { |
|
|
|
function processBlurNext( |
|
|
|
return new Promise<string>((resolve) => { |
|
|
|
img: HTMLImageElement, |
|
|
|
const canvas = document.createElement('canvas'); |
|
|
|
radius: number, |
|
|
|
|
|
|
|
iterations: number, |
|
|
|
|
|
|
|
canvas: HTMLCanvasElement = document.createElement('canvas') |
|
|
|
|
|
|
|
) { |
|
|
|
canvas.width = img.width; |
|
|
|
canvas.width = img.width; |
|
|
|
canvas.height = img.height; |
|
|
|
canvas.height = img.height; |
|
|
|
|
|
|
|
|
|
|
|
const ctx = canvas.getContext('2d', {alpha: false}); |
|
|
|
const ctx = canvas.getContext('2d', {alpha: false}); |
|
|
|
if(isFilterAvailable) { |
|
|
|
if(IS_CANVAS_FILTER_SUPPORTED) { |
|
|
|
ctx.filter = `blur(${radius}px)`; |
|
|
|
ctx.filter = `blur(${radius}px)`; |
|
|
|
ctx.drawImage(img, -radius * 2, -radius * 2, canvas.width + radius * 4, canvas.height + radius * 4); |
|
|
|
ctx.drawImage(img, -radius * 2, -radius * 2, canvas.width + radius * 4, canvas.height + radius * 4); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -36,65 +39,62 @@ function processBlurNext(img: HTMLImageElement, radius: number, iterations: numb |
|
|
|
fastBlurFunc(ctx, 0, 0, canvas.width, canvas.height, radius, iterations); |
|
|
|
fastBlurFunc(ctx, 0, 0, canvas.width, canvas.height, radius, iterations); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resolve(canvas.toDataURL()); |
|
|
|
return canvas; |
|
|
|
/* if(DEBUG) { |
|
|
|
|
|
|
|
console.log(`[blur] end, radius: ${radius}, iterations: ${iterations}, time: ${performance.now() - perf}`); |
|
|
|
|
|
|
|
} */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* canvas.toBlob(blob => { |
|
|
|
|
|
|
|
resolve(URL.createObjectURL(blob)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(DEBUG) { |
|
|
|
|
|
|
|
console.log(`[blur] end, radius: ${radius}, iterations: ${iterations}, time: ${performance.now() - perf}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); */ |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const blurPromises: Map<string, Promise<string>> = new Map(); |
|
|
|
type CacheValue = {canvas: HTMLCanvasElement, promise: Promise<void>}; |
|
|
|
const CACHE_SIZE = 1000; |
|
|
|
const cache: Map<string, CacheValue> = new Map(); |
|
|
|
|
|
|
|
const CACHE_SIZE = 150; |
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
throw 'no dataUri for blur: ' + dataUri; |
|
|
|
return Promise.resolve(dataUri); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(blurPromises.size > CACHE_SIZE) { |
|
|
|
if(cache.size > CACHE_SIZE) { |
|
|
|
blurPromises.clear(); |
|
|
|
cache.clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(blurPromises.has(dataUri)) return blurPromises.get(dataUri); |
|
|
|
const canvas = document.createElement('canvas'); |
|
|
|
const promise = new Promise<string>((resolve) => { |
|
|
|
canvas.className = 'canvas-thumbnail'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cached = cache.get(dataUri); |
|
|
|
|
|
|
|
if(!cached) { |
|
|
|
|
|
|
|
const promise: CacheValue['promise'] = new Promise((resolve) => { |
|
|
|
//return resolve(dataUri);
|
|
|
|
//return resolve(dataUri);
|
|
|
|
requireBlurPromise.then(() => { |
|
|
|
requireBlurPromise.then(() => { |
|
|
|
const img = new Image(); |
|
|
|
const img = new Image(); |
|
|
|
img.onload = () => { |
|
|
|
img.onload = () => { |
|
|
|
if(isFilterAvailable) { |
|
|
|
// if(IS_CANVAS_FILTER_SUPPORTED) {
|
|
|
|
processBlurNext(img, radius, iterations).then(resolve); |
|
|
|
// resolve(processBlurNext(img, radius, iterations));
|
|
|
|
} else { |
|
|
|
// } else {
|
|
|
|
addHeavyTask({ |
|
|
|
const promise = addHeavyTask({ |
|
|
|
items: [[img, radius, iterations]], |
|
|
|
items: [[img, radius, iterations, canvas]], |
|
|
|
context: null, |
|
|
|
context: null, |
|
|
|
process: processBlurNext |
|
|
|
process: processBlurNext |
|
|
|
}, 'unshift').then(results => { |
|
|
|
}, 'unshift'); |
|
|
|
resolve(results[0]); |
|
|
|
|
|
|
|
|
|
|
|
promise.then(() => { |
|
|
|
|
|
|
|
resolve(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
// }
|
|
|
|
}; |
|
|
|
}; |
|
|
|
img.src = dataUri; |
|
|
|
img.src = dataUri; |
|
|
|
|
|
|
|
|
|
|
|
/* addHeavyTask({ |
|
|
|
|
|
|
|
items: [[dataUri, radius, iterations]], |
|
|
|
|
|
|
|
context: null, |
|
|
|
|
|
|
|
process: processBlur |
|
|
|
|
|
|
|
}, 'unshift').then(results => { |
|
|
|
|
|
|
|
resolve(results[0]); |
|
|
|
|
|
|
|
}); */ |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
blurPromises.set(dataUri, promise); |
|
|
|
cache.set(dataUri, cached = { |
|
|
|
|
|
|
|
canvas, |
|
|
|
|
|
|
|
promise |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
canvas.width = cached.canvas.width; |
|
|
|
|
|
|
|
canvas.height = cached.canvas.height; |
|
|
|
|
|
|
|
canvas.getContext('2d').drawImage(cached.canvas, 0, 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return promise; |
|
|
|
return { |
|
|
|
|
|
|
|
...cached, |
|
|
|
|
|
|
|
canvas |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|