admin管理员组文章数量:1400614
I am struggling to add Color Thief in a react app. I have followed the instruction on github but nothing changes. I have applied the suggestions reporte here and then inserted the scripts inside a setTimeout function, but I get always the same error:
Can you please help me to run this library (or similars if you have alternatives) in react?
Here is my code so far:
import React from 'react';
import './App.css';
var ColorThief = require('color-thief');
function App() {
setTimeout(function(){
var colorThief = new ColorThief();
var img = 'img.jpg';
var palette = colorThief.getPalette(img, 8);
var dominant = colorThief.getColor(img);
console.log(palette);
console.log(dominant);
document.getElementById("app").style.backgroundColor = "rgb(" + dominant + ")";
}, 3000);
return (
<div id="app"></div>
);
}
export default App;
I am struggling to add Color Thief in a react app. I have followed the instruction on github but nothing changes. I have applied the suggestions reporte here and then inserted the scripts inside a setTimeout function, but I get always the same error:
Can you please help me to run this library (or similars if you have alternatives) in react?
Here is my code so far:
import React from 'react';
import './App.css';
var ColorThief = require('color-thief');
function App() {
setTimeout(function(){
var colorThief = new ColorThief();
var img = 'img.jpg';
var palette = colorThief.getPalette(img, 8);
var dominant = colorThief.getColor(img);
console.log(palette);
console.log(dominant);
document.getElementById("app").style.backgroundColor = "rgb(" + dominant + ")";
}, 3000);
return (
<div id="app"></div>
);
}
export default App;
Share
Improve this question
asked May 17, 2019 at 7:28
Nad GNad G
5172 gold badges10 silver badges21 bronze badges
7
-
getPalette
/getColor
doesn't expect a string as parameter. – Caramiriel Commented May 17, 2019 at 7:36 - how can I pass the image, then? – Nad G Commented May 17, 2019 at 7:51
-
For
getColor
there's agetColorFromUrl
, but since you also needgetPalette
, I suggest having a look at the source code at github./lokesh/color-thief/blob/… Create an image element, and after it's loaded, pass it down to the functions you've already used. – Caramiriel Commented May 17, 2019 at 8:02 - Unfortunatelly, that doesn't work neither. For the sake of detail I console.log the colorThief var and it turn out to be an empty object. That means that var ColorThief = require('color-thief'); doesn't return anything. Any suggestions? – Nad G Commented May 17, 2019 at 8:13
- Did you find the solution? because i'm struggling with this – Yorbjörn Commented Jul 14, 2020 at 7:02
3 Answers
Reset to default 6Install the library with npm:
$ npm i --save colorthief
Import the library in your file as:
import ColorThief from "colorthief";
Create a react reference to your image like so:
constructor(props) {
super(props);
// Image element reference
this.imgRef = React.createRef();
}
The image ponent should look like this:
<img
crossOrigin={"anonymous"}
ref={this.imgRef}
src={
"https://images.unsplash./photo-1516876437184-593fda40c7ce"
}
alt={"example"}
className={"example__img"}
onLoad={() => {
const colorThief = new ColorThief();
const img = this.imgRef.current;
const result = colorThief.getColor(img, 25);
}}
/>
( Please note, the image props should be in this exact order)
import ColorThief from "colorthief";
import _ from 'lodash';
export const getPalette = (url) => {
return new Promise((resolve, reject) => {
if (!url) {
reject();
}
const image = new Image();
image.src = url;
image.crossOrigin = 'Anonymous';
image.onload = function () {
const colorThief = new ColorThief();
const palette = colorThief.getPalette(this, 10);
const result = _.uniq(palette, item => JSON.stringify(item));
resolve(result);
}
});
}
This below code works for me. version : "colorthief": "^2.3.2",
import ColorThief from "../../../node_modules/colorthief/dist/color-thief.mjs"; // your node modules path
export const getPalette = (url) => {
return new Promise(resolve=>{
const img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = () => {
let colorThief = new ColorThief();
resolve(colorThief.getPalette(img));
}
img.src = url;
})
}
本文标签: javascriptUsing Color Thief Library in React doesn39t workStack Overflow
版权声明:本文标题:javascript - Using Color Thief Library in React doesn't work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744227815a2596180.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论