Jump to content

Recommended Posts

Seriously, HTML 5 Canvas really lacks functionality. I searched how to clear a text written on it, and all what I am getting is to clear the whole damn canvas. Moreover, clearing all itself is not working. Here are my codes:

 

HTML

 

<img src="jjj.jpg" width="252" height="144" id="image">
<canvas id="canvas" width="252"	height="144"></canvas>
<input type="button" onclick=" insertImg ();">
<input type="text" id="text"/>
<input type="button" onclick="writeText();">
<input type="button" value="Clear" onclick="clearCanvas();">

 

Javascript

 

//Draw image

function insertImg () {
 var canvas = document.getElementById("canvas");
 var context = canvas.getContext("2d");
 var image = document.getElementById("image");
 context.drawImage(image, 0, 0);
}

//Write text

function writeText () {
 var canvas = document.getElementById("canvas");
 var context = canvas.getContext("2d");
 var style = 'italic';
 var size = '50pt';
 var family = '"Arial Black", Gadget, sans-serif'
 var text = document.getElementById("text").value;
 context.font = style + " " + size + " " + family;
 context.fillStyle = 'blue';
 context.fillText(text, 50, 50);
}

//Clear canvas

function clearCanvas () {
 context.clearRect(0, 0, 252, 144);
}

 

Everything is working correctly, except, the canvas is not clearing when I click on the clear button. I will be glad if we can clear the text also separately from clearing canvas as a whole.

Link to comment
https://forums.phpfreaks.com/topic/271059-how-to-clear-text-on-html-5-canvas/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.