Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 16, 2019 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/e51f6451d74c2244554da6828fbd5cd5 to your computer and use it in GitHub Desktop.
Save parzibyte/e51f6451d74c2244554da6828fbd5cd5 to your computer and use it in GitHub Desktop.
Líneas en Canvas JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Líneas en Canvas</title>
</head>
<body>
<h1>Líneas en Canvas usando JavaScript</h1>
<a href="//parzibyte.me/blog">By Parzibyte</a>
<br>
<canvas id="canvas"></canvas>
<script src="script.js"></script>
</body>
</html>
/**
* Ejercicios con Canvas y JavaScript: líneas
*
* https://parzibyte.me/blog
*/
const ALTURA_CANVAS = 200,
ANCHURA_CANVAS = 400;
// Obtener el elemento del DOM
const canvas = document.querySelector("#canvas");
canvas.width = ANCHURA_CANVAS;
canvas.height = ALTURA_CANVAS;
// Del canvas, obtener el contexto para poder dibujar
const contexto = canvas.getContext("2d");
// Comenzar
contexto.beginPath();
// Grosor de línea
contexto.lineWidth = 3;
// Color de línea
contexto.strokeStyle = "blue";
// Comenzamos en 0, 0
contexto.moveTo(0, 0);
// Hacemos una línea hasta 48, 48
contexto.lineTo(48, 48);
contexto.stroke(); // "Guardar" cambios
// Otra línea
contexto.beginPath();
contexto.strokeStyle = "red";
contexto.moveTo(100, 0);
contexto.lineTo(52, 48);
contexto.stroke();
// Otra línea
contexto.beginPath();
contexto.strokeStyle = "yellow";
contexto.moveTo(0, 100);
contexto.lineTo(48, 52);
contexto.stroke();
// Otra línea
contexto.beginPath();
contexto.strokeStyle = "green";
contexto.moveTo(100, 100);
contexto.lineTo(52, 52);
contexto.stroke();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment