Is it possible to add img to CANVAS after window.load?

Asked

Viewed 76 times

0

Good afternoon Devs o/

I have a question, I need to insert an image to <canvas>. But that’s not all, I’m putting together a blog creation system for Instagram, so I have several templates in the database, I need to put it inside the <canvas> and so far I’ve put the photo of the template, user, name of the same and position. Getting this result:

inserir a descrição da imagem aqui

We can see that everything is going well, but the real problem is in selecting the employee that the user wants to do the post. At the time of print the selected name is Bruna, but no photo was loaded. And I say that the problem is not in PHP.

I did not put the connection information with the bank and table, because I am solving the problem in production

header("Content-type: image/gif");
if($row=mysqli_fetch_object($result)) { 
   echo "
       var canvas = document.getElementById('canvas');
       context = canvas.getContext('2d');
                    
       img4 = new Image(115,115)
       img4.src = 'data:image/png;base64,'
       img4.onload = function(){
          context.drawImage(img4, 201, 128, img4.width, img4.height)
          context.font = '11px Montserrat';
          context.fillText('".$row->nome_consultor."', 222, 258);
       } 
    "
}

When going to the console and placing img4, it returns the tag img with all attributes I requested for PHP, but html is not 'showing' the content inserted in <canvas>.

Does anyone know if the canvas have post-loading limitations? Or actually have some syntax problem?

Thanks in advance <3

1 answer

0


Hello. It seems that your code has confusion between backend and frontend.

You are informing that the output of your PHP will be a gif:

header("Content-type: image/gif");

But you’re writing JS code:

echo "
       var canvas = document.getElementById('canvas');
       context = canvas.getContext('2d');
                    
       img4 = new Image(115,115)
       img4.src = 'data:image/png;base64,'
       img4.onload = function(){
          context.drawImage(img4, 201, 128, img4.width, img4.height)
          context.font = '11px Montserrat';
          context.fillText('".$row->nome_consultor."', 222, 258);
       } 
    "

I honestly couldn’t understand much of what you want to do, but I can state for sure that either your header is wrong.

  • Come on, it makes perfect sense what you said, but I make an ajax request to recover the template image. After that it puts everything I need to leave the template ready, as seen in the first image, after that the user needs to select the employee she wants and then on onChange of select i perform another ajax request to return me the image of the selected employee and until then was to add the image along the name to the context of canvas. All JS that is 'inside' of PHP will be added in a tag <script>

  • That is, when he puts the content inside the canvas it is not visible, but it does exist! When I recover the variable img4 in the console it returns exactly the img with the parameters defined by PHP, such as src, width and height, but for some reason it does not exist in the DOM of canvas

  • If you are going to return a JS that must be executed when responding to the ajax object, you must set the correct mime-type in the header, which if I’m not mistaken is text/javascript. Then, upon receiving the reply, it uses the Eval() method. It would be Eval(responseText).

  • The others header are in this same format and both show the image I need, but at some point the DOM of canvas loses the img4 and puts only the template, user photo, name and position of the same.

  • I imagine that after defining the context of canvas in another request causes other contexts added in another plane not to exist

  • I’ll try to change some things and I’ll get back to you

  • 1

    I’m not sure I’m 100% right, but it was exactly what I said, I passed the same parameters to the ajax that requests the employee’s photo, what it does is simple, search the template again, (inserts the user photo, name and position, "ALLOCATED IN $_SESSION") All right, just like I thought! I appreciate your help anyway, I really needed to hear what you said to give up what I was doing and redo it all again

  • Any doubt, we’re there

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.