Update index.js

This commit is contained in:
Harvey Zack authored and GitHub committed 2019-04-18 21:48:24 +08:00
1 parent 662531b35f
commit b787a3bb99
1 file changed
+3 -8
@@ -12,12 +12,12 @@ function matrixCallback(callback) {
return result; return result;
} }
function getColors($img, width, height) { function getColors($video, width, height) {
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
ctx.drawImage($img, 0, 0); ctx.drawImage($video, 0, 0);
return matrixCallback((xIndex, yIndex, x, y) => { return matrixCallback((xIndex, yIndex, x, y) => {
const itemW = width / x; const itemW = width / x;
const itemH = height / y; const itemH = height / y;
@@ -87,13 +87,10 @@ function artplayerPluginBacklight(art) {
art.on('firstCanplay', () => { art.on('firstCanplay', () => {
const { clientWidth, clientHeight } = $video; const { clientWidth, clientHeight } = $video;
const matrix = creatMatrix($backlight, clientWidth, clientHeight); const matrix = creatMatrix($backlight, clientWidth, clientHeight);
const $img = document.createElement('img');
(function loop() { (function loop() {
window.requestAnimationFrame(() => { window.requestAnimationFrame(() => {
if (player.playing) { if (player.playing) {
const dataUri = player.getScreenshotDataURL(); const colors = getColors($video, clientWidth, clientHeight);
$img.onload = () => {
const colors = getColors($img, clientWidth, clientHeight);
colors.forEach(({ r, g, b }, index) => { colors.forEach(({ r, g, b }, index) => {
const { $box, left, right, top, bottom } = matrix[index]; const { $box, left, right, top, bottom } = matrix[index];
// eslint-disable-next-line no-nested-ternary // eslint-disable-next-line no-nested-ternary
@@ -104,8 +101,6 @@ function artplayerPluginBacklight(art) {
$box.style.boxShadow = `rgb(${r}, ${g}, ${b}) ${x} ${y} 128px`; $box.style.boxShadow = `rgb(${r}, ${g}, ${b}) ${x} ${y} 128px`;
}); });
}; };
$img.src = dataUri;
};
loop(); loop();
}); });
})(); })();