优化背光刷新频率

This commit is contained in:
zhw2590582 committed 2020-02-29 23:03:44 +08:00
1 parent c7a351bc55
commit 2d4da12180
3 files changed
+10 -10

No files matched your search

@@ -101,7 +101,7 @@
$player.insertBefore($backlight, $video);
(function loop() {
window.requestAnimationFrame(function () {
setTimeout(function () {
if (player.playing) {
var clientWidth = $video.clientWidth,
clientHeight = $video.clientHeight;
@@ -128,7 +128,7 @@
if (!art.isDestroy) {
loop();
}
});
}, 200);
})();
return {
@@ -1 +1 @@
{"version":3,"file":"artplayer-plugin-backlight.js","sources":["../../packages/artplayer-plugin-backlight/src/index.js"],"sourcesContent":["function matrixCallback(callback) {\n const result = [];\n const x = 10;\n const y = 5;\n for (let xIndex = 0; xIndex < x; xIndex += 1) {\n for (let yIndex = 0; yIndex < y; yIndex += 1) {\n if (xIndex === 0 || xIndex === x - 1 || yIndex === 0 || yIndex === y - 1) {\n result.push(callback(xIndex, yIndex, x, y));\n }\n }\n }\n return result;\n}\n\nfunction getColors($canvas, $video, width, height) {\n const ctx = $canvas.getContext('2d');\n $canvas.width = width;\n $canvas.height = height;\n ctx.drawImage($video, 0, 0);\n return matrixCallback((xIndex, yIndex, x, y) => {\n const itemW = width / x;\n const itemH = height / y;\n const itemX = xIndex * itemW;\n const itemY = yIndex * itemH;\n const { data } = ctx.getImageData(itemX, itemY, itemW, itemH);\n let r = 0;\n let g = 0;\n let b = 0;\n for (let i = 0, l = data.length; i < l; i += 4) {\n r += data[i];\n g += data[i + 1];\n b += data[i + 2];\n }\n r = Math.floor(r / (data.length / 4));\n g = Math.floor(g / (data.length / 4));\n b = Math.floor(b / (data.length / 4));\n return { r, g, b };\n });\n}\n\nfunction creatMatrix(parent) {\n return matrixCallback((xIndex, yIndex, x, y) => {\n const $box = document.createElement('div');\n $box.style.position = 'absolute';\n $box.style.left = `${xIndex * 100 / x}%`;\n $box.style.top = `${yIndex * 100 / y}%`;\n $box.style.width = `${100 / x}%`;\n $box.style.height = `${100 / y}%`;\n $box.style.webkitBorderRadius = '50%';\n $box.style.borderRadius = '50%';\n $box.style.webkitTransition = 'all .2s ease';\n $box.style.transition = 'all .2s ease';\n parent.appendChild($box);\n return {\n $box,\n left: xIndex === 0,\n right: xIndex === x - 1,\n top: yIndex === 0,\n bottom: yIndex === y - 1,\n };\n });\n}\n\nfunction artplayerPluginBacklight(art) {\n const { setStyles } = art.constructor.utils;\n const {\n template: { $player, $video },\n player,\n } = art;\n\n const $backlight = document.createElement('div');\n $backlight.classList.add('artplayer-backlight');\n setStyles($backlight, {\n position: 'absolute',\n zIndex: 9,\n left: 0,\n top: 0,\n right: 0,\n bottom: 0,\n width: '100%',\n height: '100%',\n });\n\n const matrix = creatMatrix($backlight);\n const $canvas = document.createElement('canvas');\n $player.insertBefore($backlight, $video);\n\n (function loop() {\n window.requestAnimationFrame(() => {\n if (player.playing) {\n const { clientWidth, clientHeight } = $video;\n const colors = getColors($canvas, $video, clientWidth, clientHeight);\n colors.forEach(({ r, g, b }, index) => {\n const { $box, left, right, top, bottom } = matrix[index];\n // eslint-disable-next-line no-nested-ternary\n const x = left ? '-64px' : right ? '64px' : '0';\n // eslint-disable-next-line no-nested-ternary\n const y = top ? '-64px' : bottom ? '64px' : '0';\n $box.style.webkitBoxShadow = `rgb(${r}, ${g}, ${b}) ${x} ${y} 128px`;\n $box.style.boxShadow = `rgb(${r}, ${g}, ${b}) ${x} ${y} 128px`;\n }); \n };\n\n if (!art.isDestroy) {\n loop();\n }\n });\n })();\n\n return {\n name: 'artplayerPluginBacklight',\n };\n}\n\nexport default artplayerPluginBacklight;"],"names":["matrixCallback","callback","result","x","y","xIndex","yIndex","push","getColors","$canvas","$video","width","height","ctx","getContext","drawImage","itemW","itemH","itemX","itemY","getImageData","data","r","g","b","i","l","length","Math","floor","creatMatrix","parent","$box","document","createElement","style","position","left","top","webkitBorderRadius","borderRadius","webkitTransition","transition","appendChild","right","bottom","artplayerPluginBacklight","art","setStyles","constructor","utils","template","$player","player","$backlight","classList","add","zIndex","matrix","insertBefore","loop","window","requestAnimationFrame","playing","clientWidth","clientHeight","colors","forEach","index","webkitBoxShadow","boxShadow","isDestroy","name"],"mappings":";;;;;;IAAA,SAASA,cAAT,CAAwBC,QAAxB,EAAkC;IAC9B,MAAMC,MAAM,GAAG,EAAf;IACA,MAAMC,CAAC,GAAG,EAAV;IACA,MAAMC,CAAC,GAAG,CAAV;;IACA,OAAK,IAAIC,MAAM,GAAG,CAAlB,EAAqBA,MAAM,GAAGF,CAA9B,EAAiCE,MAAM,IAAI,CAA3C,EAA8C;IAC1C,SAAK,IAAIC,MAAM,GAAG,CAAlB,EAAqBA,Line truncated
{"version":3,"file":"artplayer-plugin-backlight.js","sources":["../../packages/artplayer-plugin-backlight/src/index.js"],"sourcesContent":["function matrixCallback(callback) {\n const result = [];\n const x = 10;\n const y = 5;\n for (let xIndex = 0; xIndex < x; xIndex += 1) {\n for (let yIndex = 0; yIndex < y; yIndex += 1) {\n if (xIndex === 0 || xIndex === x - 1 || yIndex === 0 || yIndex === y - 1) {\n result.push(callback(xIndex, yIndex, x, y));\n }\n }\n }\n return result;\n}\n\nfunction getColors($canvas, $video, width, height) {\n const ctx = $canvas.getContext('2d');\n $canvas.width = width;\n $canvas.height = height;\n ctx.drawImage($video, 0, 0);\n return matrixCallback((xIndex, yIndex, x, y) => {\n const itemW = width / x;\n const itemH = height / y;\n const itemX = xIndex * itemW;\n const itemY = yIndex * itemH;\n const { data } = ctx.getImageData(itemX, itemY, itemW, itemH);\n let r = 0;\n let g = 0;\n let b = 0;\n for (let i = 0, l = data.length; i < l; i += 4) {\n r += data[i];\n g += data[i + 1];\n b += data[i + 2];\n }\n r = Math.floor(r / (data.length / 4));\n g = Math.floor(g / (data.length / 4));\n b = Math.floor(b / (data.length / 4));\n return { r, g, b };\n });\n}\n\nfunction creatMatrix(parent) {\n return matrixCallback((xIndex, yIndex, x, y) => {\n const $box = document.createElement('div');\n $box.style.position = 'absolute';\n $box.style.left = `${(xIndex * 100) / x}%`;\n $box.style.top = `${(yIndex * 100) / y}%`;\n $box.style.width = `${100 / x}%`;\n $box.style.height = `${100 / y}%`;\n $box.style.webkitBorderRadius = '50%';\n $box.style.borderRadius = '50%';\n $box.style.webkitTransition = 'all .2s ease';\n $box.style.transition = 'all .2s ease';\n parent.appendChild($box);\n return {\n $box,\n left: xIndex === 0,\n right: xIndex === x - 1,\n top: yIndex === 0,\n bottom: yIndex === y - 1,\n };\n });\n}\n\nfunction artplayerPluginBacklight(art) {\n const { setStyles } = art.constructor.utils;\n const {\n template: { $player, $video },\n player,\n } = art;\n\n const $backlight = document.createElement('div');\n $backlight.classList.add('artplayer-backlight');\n setStyles($backlight, {\n position: 'absolute',\n zIndex: 9,\n left: 0,\n top: 0,\n right: 0,\n bottom: 0,\n width: '100%',\n height: '100%',\n });\n\n const matrix = creatMatrix($backlight);\n const $canvas = document.createElement('canvas');\n $player.insertBefore($backlight, $video);\n\n (function loop() {\n setTimeout(() => {\n if (player.playing) {\n const { clientWidth, clientHeight } = $video;\n const colors = getColors($canvas, $video, clientWidth, clientHeight);\n colors.forEach(({ r, g, b }, index) => {\n const { $box, left, right, top, bottom } = matrix[index];\n // eslint-disable-next-line no-nested-ternary\n const x = left ? '-64px' : right ? '64px' : '0';\n // eslint-disable-next-line no-nested-ternary\n const y = top ? '-64px' : bottom ? '64px' : '0';\n $box.style.webkitBoxShadow = `rgb(${r}, ${g}, ${b}) ${x} ${y} 128px`;\n $box.style.boxShadow = `rgb(${r}, ${g}, ${b}) ${x} ${y} 128px`;\n });\n }\n\n if (!art.isDestroy) {\n loop();\n }\n }, 200);\n })();\n\n return {\n name: 'artplayerPluginBacklight',\n };\n}\n\nexport default artplayerPluginBacklight;\n"],"names":["matrixCallback","callback","result","x","y","xIndex","yIndex","push","getColors","$canvas","$video","width","height","ctx","getContext","drawImage","itemW","itemH","itemX","itemY","getImageData","data","r","g","b","i","l","length","Math","floor","creatMatrix","parent","$box","document","createElement","style","position","left","top","webkitBorderRadius","borderRadius","webkitTransition","transition","appendChild","right","bottom","artplayerPluginBacklight","art","setStyles","constructor","utils","template","$player","player","$backlight","classList","add","zIndex","matrix","insertBefore","loop","setTimeout","playing","clientWidth","clientHeight","colors","forEach","index","webkitBoxShadow","boxShadow","isDestroy","name"],"mappings":";;;;;;IAAA,SAASA,cAAT,CAAwBC,QAAxB,EAAkC;IAC9B,MAAMC,MAAM,GAAG,EAAf;IACA,MAAMC,CAAC,GAAG,EAAV;IACA,MAAMC,CAAC,GAAG,CAAV;;IACA,OAAK,IAAIC,MAAM,GAAG,CAAlB,EAAqBA,MAAM,GAAGF,CAA9B,EAAiCE,MAAM,IAAI,CAA3C,EAA8C;IAC1C,SAAK,IAAIC,MAAM,GAAG,CAAlB,EAAqBA,MAAM,GAAGF,CAA9B,EAAiCE,MAAM,IAALine truncated