Html 5 Canvas Text App Part II
Last time we made the Canvas text app.This time we will add shadows and download functionality to it. Lets get started.
If you haven't seen the first part of this series , I recommend you to do so here...,
HTML 5 Shadows
In html 5 you can give shadows not only to the text. HTML 5 Shadows provides four properties make awesome effects.
context.shadowColor
For the color of the shadow.Color is given in the format “#RRGGBB”.
context.shadowOffsetX
A positive or negative number showing x offset of shadow.
context.shadowOffsetY
A positive or negative number showing x offset of shadow.
context.shadowBlur
Provides blur filter for diffusion of the shadow. The higher the value, the more diffusion.
An example of it can be
context.shadowColor = "#00FF00";
context.shadowOffsetX = 4;
context.shadowOffsetY = 4;
context.shadowBlur = 1;
This will produce this result...,
Now when you understand the concepts of shadows lets work on the app.
THE IMPLEMENTATION
Lets make some inputs for shadows.
Now lets add functionality to this code
We will call this function in draw() function just like the drawbg() functionality.
Now lets create the download section so that we can download the end result of the slide.
Add Functionality to it....,
Here is the End result...,
Here is the full code.....,
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body onload="Draw()">
<div>
<canvas width="600" height="400" id="Can">Canvas not supported</canvas>
</div>
<div>
<table border="10px">
<tr>
<td>
<h4>Text</h4>
<textarea id="MainText">Hello Canvas</textarea>
<button onclick="Draw()">Draw Text</button>
</td>
<td>
<h4>Font</h4>
<input type="text" value="50px Arial" id="font">
<button onclick="Draw()">Draw Text</button>
</td>
</tr>
<tr>
<td>
<span id="Rangex"></span>
<br>
<span>Position X :</span><input type="range" id="rangex" max = "600" oninput="Draw()">
</td>
<td>
<span id="Rangey"></span>
<br>
<span>Position Y :</span><input type="range" id="rangey" max = "400" oninput="Draw()">
</td>
</tr>
<tr>
<td>
<h4>Text Color</h4>
<input type="color" value="#ffffff" id="ColorChooserText">
<button onclick="Draw()">Change Color</button>
</td>
<td>
<h4>Background Color</h4>
<input type="color" value="#ff0000" id="ColorChooserBG">
<button onclick="drawBG()">Draw Background</button>
<Br>
</td>
</tr>
<tr>
<td>
<span>Shadow X :</span><input type="number" id="ShadowoffX" oninput="Draw()">
<br>
<span>Shadow Y :</span><input type="number" id="ShadowoffY" oninput="Draw()">
<br>
<span>Shadow Blur :</span><input type="number" id="ShadowoBlur" oninput="Draw()">
<br>
<span>Shadow Color :</span><input type="color" value="#000000" id="ShadowColor" oninput="Draw()">
</td>
<td>
<p>Click Here to Save Canvas</p>
<a id ="DownloadCanvas" download="Canvas.jpg">Download Canvas</a>
</td>
</tr>
</table>
</div>
<script>
//To make tutorial of it we are going to copy commands one by one and make a ste
//by step
var Srangex = document.getElementById("Rangex");
var Srangey = document.getElementById("Rangey");
var canvas = document.getElementById("Can");
var c = canvas.getContext("2d");
//EventListener
DownloadCanvas.addEventListener("click",canvassave,false);
function Draw()
{
//For Range Input
var rangex = document.getElementById("rangex").value;
var rangey = document.getElementById("rangey").value;
//For Text
var text = document.getElementById("MainText").value;
c.clearRect(0,0,canvas.width,canvas.height);
//To Draw Background
drawBG();
//Draw Shadow
DrawShadow();
//For Text
c.fillStyle = document.getElementById("ColorChooserText").value;
c.font = document.getElementById("font").value;
c.fillText(text,rangex,rangey);
//To print the values
Srangex.innerHTML = "Position: " + rangex;
Srangey.innerHTML = "Position: " + rangey;
}
function canvassave()
{
var dataurl = canvas.toDataURL();
this.href = dataurl;
}
//drawBG function
function drawBG()
{
c.fillStyle = document.getElementById("ColorChooserBG").value;
c.fillRect(0,0,Can.width,Can.height);
}
//Draw Shadow
function DrawShadow()
{
c.shadowOffsetX = document.getElementById("ShadowoffX").value;
c.shadowOffsetY = document.getElementById("ShadowoffY").value;
c.shadowColor = document.getElementById("ShadowColor").value;
c.shadowBlur = document.getElementById("ShadowoBlur").value;
}
</script>
</body>
</html>
Thank you very much for your support.
Please Subscribe to this blog or follow me on google plus in top left corner for more stuff like this.
Comments
Post a Comment