O
O
olol7772015-11-01 05:24:58
JavaScript
olol777, 2015-11-01 05:24:58

Canvas Ctx.fillStyle how to make dynamic color?

Hello! The essence is this, the text is taken from the form and converted into a picture by this script:

var tCtx = document.getElementById('textCanvas').getContext('2d'),
    imageElem = document.getElementById('image');
document.getElementById('sign').addEventListener('keyup', function (){
    tCtx.canvas.width = tCtx.measureText(this.value).width;
    tCtx.font = "50px serif"; 
    tCtx.fillStyle = "#FFFFFF";
    tCtx.fillText (this.value, 0, 80);
    imageElem.src = tCtx.canvas.toDataURL();
    console.log(imageElem.src);
}, false);

There are 3 image buttons, each has a data-color:
<img id="black" class="activeColor" data-color="black" data-colorreal="Черный" src="images/icon_black.png" style="border-radius: 50px; box-shadow: rgba(122, 122, 122, 0.901961) 0.1em 0.1em 9px;" hspace="10" vspace="10">
    <img id="white" data-color="white" data-colorreal="Белый" src="images/icon_white.png" style="border-radius: 50px; box-shadow: rgba(122, 122, 122, 0.901961) 0.1em 0.1em 9px; " hspace="10" vspace="10">
    <img id="grey" data-color="white" data-colorreal="Серый" src="images/icon_grey.png" style="border-radius: 50px; box-shadow: rgba(122, 122, 122, 0.901961) 0.1em 0.1em 9px;" hspace="10" vspace="10">

How to make tCtx.fillStyle change when clicking on the button depending on the data-color?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2015-11-01
@olol777

Alternatively https://jsfiddle.net/twobomb/pusxgghv/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question