最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Color Thief Library in React doesn't work - Stack Overflow

programmeradmin5浏览0评论

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 a getColorFromUrl, but since you also need getPalette, 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
 |  Show 2 more ments

3 Answers 3

Reset to default 6

Install 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;
    })
}
发布评论

评论列表(0)

  1. 暂无评论