Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Una de las características fundamentales del canvas, es que podemos dibujar en él. Pero antes de iniciar a dibujar en el canvas, borremos la sentencia
alert(“Bienvenidos al canvas”); del archivo de la sección anterior.
<body>
<h1>Arcos CANVAS</h1>
<canvas id="miCanvas" width="500px" height="300px">
Tu navegador no soporta CANVAS
</canvas>
</body>
</html>
Dibujando curvas bézier <!doctype html> canvasBase08.html
<html>
<head>
<meta charset="UTF-8">
<title>CANVAS</title>
<style>
#miCanvas{
border:dotted 2px yellow;
background:green;
}
</style>
<script>
window.onload = function(){
var canvas = document.getElementById("miCanvas");
if(canvas && canvas.getContext){
var ctx = canvas.getContext("2d");
if(ctx){
//Colores
ctx.lineWidth = 10;
ctx.strokeStyle = "red";
ctx.fillStyle = "yellow";
//Curva bezier
ctx.beginPath();
//Primer punto
ctx.moveTo(50,20);
//Dibujamos la curva
ctx.bezierCurveTo(50,100, 200,100, 200,150);
//Dibujamos la curva
ctx.stroke();
//Primer punto
ctx.fillRect(50,20,5,5);
} else {
alert("Error al crear tu contexto");
}
}
}
</script>
</head>
<body>
<h1>Curvas bézier</h1>
<canvas id="miCanvas" width="500px" height="300px">
Tu navegador no soporta CANVAS
</canvas>
</body>
</html>
Los gradientes lineales <!doctype html> canvasBase09.html
en el canvas <html>
<head>
<meta charset="UTF-8">
<title>CANVAS</title>
<style>
#miCanvas{
border:dotted 2px yellow;
background:green;
}
</style>
<script>
window.onload = function(){
var canvas = document.getElementById("miCanvas");
if(canvas && canvas.getContext){
var ctx = canvas.getContext("2d");
if(ctx){
var gradiente = ctx.createLinearGradient(0,0,canvas.width,0);
gradiente.addColorStop(0, "green");
gradiente.addColorStop(0.35, "white");
gradiente.addColorStop(0.65, "white");
gradiente.addColorStop(1, "red");
ctx.fillStyle = gradiente;
ctx.fillRect(0,0,canvas.width,canvas.height);
} else {
alert("Error al crear tu contexto");
}
}
}
</script>
</head>
<body>
<h1>Gradientes lineales</h1>
<canvas id="miCanvas" width="500px" height="300px">
Tu navegador no soporta CANVAS
</canvas>
</body>
</html>
Diferentes direcciones <!doctype html> canvasBase10.html
del canvas <html>
<head>
<meta charset="UTF-8">
<title>CANVAS</title>
<style>
#miCanvas{
border:dotted 2px yellow;
background:green;
}
</style>
<script>
window.onload = function(){
var canvas = document.getElementById("miCanvas");
if(canvas && canvas.getContext){
var ctx = canvas.getContext("2d");
if(ctx){
var h = canvas.height;
var w = canvas.width;
var gradiente = ctx.createLinearGradient(0,0,w/2,0);
gradiente.addColorStop(0, "pink");
gradiente.addColorStop(0.15, "yellow");
gradiente.addColorStop(0.35, "white");
gradiente.addColorStop(0.65, "yellow");
gradiente.addColorStop(1, "red");
ctx.fillStyle = gradiente;
ctx.fillRect(0,0,w/2,h/2);
//Gradiente vertical
gradiente = ctx.createLinearGradient(0,0,0,h/2);
gradiente.addColorStop(0, "pink");
gradiente.addColorStop(0.15, "yellow");
gradiente.addColorStop(0.35, "white");
gradiente.addColorStop(0.65, "yellow");
gradiente.addColorStop(1, "red");
ctx.fillStyle = gradiente;
ctx.fillRect(w/2,0,w/2,h/2);
//Diagonal 1
gradiente = ctx.createLinearGradient(0,h/2,w/2,h);
gradiente.addColorStop(0, "pink");
gradiente.addColorStop(0.15, "yellow");
gradiente.addColorStop(0.35, "white");
gradiente.addColorStop(0.65, "yellow");
gradiente.addColorStop(1, "red");
ctx.fillStyle = gradiente;
ctx.fillRect(0,h/2,w/2,h);
//Diagonal 2
gradiente = ctx.createLinearGradient(w/2,h,w,h/2);
gradiente.addColorStop(0, "pink");
gradiente.addColorStop(0.15, "yellow");
gradiente.addColorStop(0.35, "white");
gradiente.addColorStop(0.65, "yellow");
gradiente.addColorStop(1, "red");
ctx.fillStyle = gradiente;
ctx.fillRect(w/2,h/2,w,h);
} else {
alert("Error al crear tu contexto");
}
}
}
</script>
</head>
<body>
<h1>Diferentes direcciones de gradientes</h1>
<canvas id="miCanvas" width="500px" height="300px">
Tu navegador no soporta CANVAS
</canvas>
</body>
</html>
Gradientes radiales <!doctype html> canvasBase11.html
<html>
<head>
<meta charset="UTF-8">
<title>CANVAS</title>
<style>
#miCanvas{
border:dotted 2px yellow;
background:green;
}
</style>
<script>
window.onload = function(){
var canvas = document.getElementById("miCanvas");
if(canvas && canvas.getContext){
var ctx = canvas.getContext("2d");
if(ctx){
var gradiente = ctx.createRadialGradient(canvas.width/2,
canvas.height,10,canvas.width/2,0,100);
gradiente.addColorStop(0, "pink");
gradiente.addColorStop(0.15, "yellow");
gradiente.addColorStop(0.35, "white");
gradiente.addColorStop(0.65, "yellow");
gradiente.addColorStop(1, "red");
ctx.fillStyle = gradiente;
ctx.fillRect(0,0,canvas.width, canvas.height);
ctx.fillStyle = gradiente;
ctx.fillRect(0,0,canvas.width, canvas.height);
} else {
alert("Error al crear tu contexto");
}
}
}
</script>
</head>
<body>
<h1>Gradientes radiales</h1>
<canvas id="miCanvas" width="500px" height="300px">
Tu navegador no soporta CANVAS
</canvas>
</body>
</html>
Como se observa deberás generar 5 páginas con la intención de observar diversos objetos que se pueden dibujar en un canvas empleando los códigos JavaScript