如何在画布上的元素上添加类?

时间:2018-09-04 14:09:27

标签: javascript reactjs css3 class canvas

我想在我的代码的FirstImg中添加一个类,以便可以在CSS中对其进行修改,但是它不起作用:

    onImageFetch = () =>
    {
        const   imageFirst = 'myfirstimg.jpg';
        const   imgSecond = 'mysecondimg.jpg';
        const   canvas = this.canvas;
        const   img = new Image();
        const   secondImg = new Image();

        img.height = 400;
        img.width = 400;
        img.padding = 10;
        img.classList.add('image-canvas'); // Here I want to add my class
        secondImg.padding = 10;
        img.onload = () =>
        {
            let     ctx = canvas.getContext('2d');
            const       width = img.width;
            const       height = img.height;

            ctx = canvas.getContext('2d');
            ctx.drawImage(img, 0, 0, width, height);
            ctx.drawImage(secondImg, 260, 30, 70, 25);
        };
        img.src = imageFirst;
        secondImg.src = imgSecond;
    }

我的CSS将是这样的:

.myNewClassCreate {
height: 50px;
width: 50px;
}

我尝试了img.className ='myclass';和img.class ='myclass';但它不起作用。 需要一些帮助,谢谢!

0 个答案:

没有答案