Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

javascript - Change Image Design color Fabric js

1i have to change active object image color currently my t-shirt color is changing but when i add design on it i am unable to change color of.

My Code To change Tshirt Color :

//setup front side canvas
canvas = new fabric.Canvas('tcanvas', {
    hoverCursor: 'pointer',
    selection: true,
    selectionBorderColor: 'blue'
});
canvas.on({
    'object:moving': function(e) {
        e.target.opacity = 0.5;
    },
    'object:modified': function(e) {
        e.target.opacity = 1;
    },
    'object:selected': onObjectSelected,
    'selection:cleared': onSelectedCleared
});
$('.color-preview').click(function(){
    var color = $(this).css("background-color");
    document.getElementById("shirtDiv").style.backgroundColor = color;
    $('.shirtDivs').css('background-color',color);
});

HERE I Try To change Design color

$('#image-color').miniColors({
    change: function(hex, rgb) {
        var activeObject = canvas.getActiveObject();
        if (activeObject && activeObject.type === 'image') {
            activeObject.fill = this.value;
            canvas.renderAll();

        }
    },
    open: function(hex, rgb) {

    },
    close: function(hex, rgb) {

    }
});

Demo

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@user3810894 - I'm guessing what you are asking is how to change the background color of the design (gravatar in your demo).

enter image description here

The reason it is not taking on the color on the shirt background is because the gravatar's background color is white and not transparent.

enter image description here instead of enter image description here

If you were to take those images and clear out the background and resave (as a PNG with transparency) then it would take on the color of the shirt when you overlayed it.

enter image description here

If you want to change the background color of the avatar different than the color of the shirt, then you will do the same thing - make the background transparent and then give it a background color with CSS (like how it works for the shirt in the demo)

The shirt changes "color" because it's only a transparent image of a shirt with shadows using varying degrees of transparency.

enter image description here

So in essence you could do it to your gravatar images as well.

Hope that helps someone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...