From 44c65523325f9b8f52bb9624014053669524d0ef Mon Sep 17 00:00:00 2001 From: Soptq <32592090+Soptq@users.noreply.github.com> Date: Sat, 23 May 2020 11:32:25 +0800 Subject: [PATCH] docs: update 'self-adapting theme according to cover' (#288) --- demo/demo.js | 17 ++++++++++++++--- docs/README.md | 21 ++++++++++++++++----- docs/zh-Hans/README.md | 17 ++++++++++++++--- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/demo/demo.js b/demo/demo.js index 46b65ef..70310a1 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -136,11 +136,22 @@ const ap5 = new APlayer({ }] }); const colorThief = new ColorThief(); +const image = new Image(); +const xhr = new XMLHttpRequest(); const setTheme = (index) => { if (!ap5.list.audios[index].theme) { - colorThief.getColorAsync(ap5.list.audios[index].cover, function (color) { - ap5.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index); - }); + xhr.onload = function(){ + let coverUrl = URL.createObjectURL(this.response); + image.onload = function(){ + let color = colorThief.getColor(image); + ap5.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index); + URL.revokeObjectURL(coverUrl) + }; + image.src = coverUrl; + } + xhr.open('GET', ap5.list.audios[index].cover, true); + xhr.responseType = 'blob'; + xhr.send(); } }; setTheme(ap5.list.index); diff --git a/docs/README.md b/docs/README.md index 8f27878..26e24aa 100644 --- a/docs/README.md +++ b/docs/README.md @@ -555,16 +555,27 @@ const ap = new APlayer({ }); const colorThief = new ColorThief(); +const image = new Image(); +const xhr = new XMLHttpRequest(); const setTheme = (index) => { if (!ap.list.audios[index].theme) { - colorThief.getColorAsync(ap.list.audios[index].cover, function (color) { - ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index); - }); + xhr.onload = function(){ + let coverUrl = URL.createObjectURL(this.response); + image.onload = function(){ + let color = colorThief.getColor(image); + ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index); + URL.revokeObjectURL(coverUrl) + }; + image.src = coverUrl; + } + xhr.open('GET', ap.list.audios[index].cover, true); + xhr.responseType = 'blob'; + xhr.send(); } }; setTheme(ap.list.index); -ap.on('listswitch', (data) => { - setTheme(data.index); +ap.on('listswitch', (index) => { + setTheme(index); }); ``` diff --git a/docs/zh-Hans/README.md b/docs/zh-Hans/README.md index 40e4d0f..ba49230 100644 --- a/docs/zh-Hans/README.md +++ b/docs/zh-Hans/README.md @@ -555,11 +555,22 @@ const ap = new APlayer({ }); const colorThief = new ColorThief(); +const image = new Image(); +const xhr = new XMLHttpRequest(); const setTheme = (index) => { if (!ap.list.audios[index].theme) { - colorThief.getColorAsync(ap.list.audios[index].cover, function (color) { - ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index); - }); + xhr.onload = function(){ + let coverUrl = URL.createObjectURL(this.response); + image.onload = function(){ + let color = colorThief.getColor(image); + ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index); + URL.revokeObjectURL(coverUrl) + }; + image.src = coverUrl; + } + xhr.open('GET', ap.list.audios[index].cover, true); + xhr.responseType = 'blob'; + xhr.send(); } }; setTheme(ap.list.index);