HTML, CSS och Javascript: Skillnad mellan sidversioner
Hoppa till navigering
Hoppa till sök
Hakan (diskussion | bidrag) |
Hakan (diskussion | bidrag) |
||
Rad 1 130: | Rad 1 130: | ||
}} | }} | ||
{{Lista | | === html-koden === | ||
{{Lista | index.html | |||
<pre> | <pre> | ||
<!DOCTYPE html> | <!DOCTYPE html> | ||
<html> | <html> | ||
<head> | <head> | ||
<title> | <meta charset="utf-8"> | ||
<title>CLICK and DRAW</title> | |||
<style> | |||
body { | |||
overflow: hidden; | |||
background-color: lightgrey | |||
} | |||
</style> | |||
</head> | </head> | ||
<body> | <body> | ||
<canvas>Please update your browser</canvas> | |||
<script src="main.js"></script> | |||
</body> | |||
</html> | |||
</pre> | |||
}} | |||
=== Javascriptet === | |||
< | {{Lista | main.js | ||
<pre> | |||
// Thanks to Jagadesha NH: | |||
// https://medium.com/@jagadeshanh/html5-canvas-click-and-draw-f665e02f5744 | |||
var canvas = document. | var canvas = document.getElementsByTagName("canvas")[0]; | ||
var | var context = canvas.getContext("2d"); | ||
var height = canvas.height = window.innerHeight; | |||
var width = canvas.width = window.innerWidth; | |||
var mouseClicked = false, mouseReleased = true; | |||
document.addEventListener("click", onMouseClick, false); | |||
document.addEventListener("mousemove", onMouseMove, false); | |||
function onMouseClick(e) { | |||
mouseClicked = !mouseClicked; | |||
} | |||
function onMouseMove(e) { | |||
if (mouseClicked) { | |||
context.beginPath(); | |||
context.arc(e.clientX, e.clientY, 7.5, 0, Math.PI * 2, false); | |||
context.lineWidth = 5; | |||
context.strokeStyle = color; | |||
context.stroke(); | |||
} | |||
} | |||
// Här börjar koden inspirerad av spelprogrammering.nu | |||
var x0 = 50; | var x0 = 50; | ||
y0 = 30; | y0 = 30; | ||
Rad 1 158: | Rad 1 191: | ||
size = 22; | size = 22; | ||
function myRectangle(x, y, w, l, color) { | function myRectangle(x, y, w, l, color) | ||
{ | |||
context.fillStyle = color | |||
context.fillRect(x, y, w, l); | |||
} | } | ||
function myCircle(x, y, r, t, color) { | function myCircle(x, y, r, t, color) | ||
{ | |||
context.beginPath(); | |||
context.arc(x,y,r,t, 2.2*Math.PI); | |||
context.fillStyle = color; | |||
context.fill(); | |||
} | } | ||
function myText(x, y, size, text, color) | function myText(x, y, size, text, color) | ||
{ | { | ||
context.font = size + "pt Helvetica"; | |||
context.fillStyle = color; | |||
context.fillText(text, x, y); | |||
} | } | ||
// rita de färgade rutorna | |||
this.myRectangle(x0, y0, bredd, hojd, "green"); | this.myRectangle(x0, y0, bredd, hojd, "green"); | ||
this.myRectangle(x0, y0 + distance, bredd, hojd, "blue"); | this.myRectangle(x0, y0 + distance, bredd, hojd, "blue"); | ||
Rad 1 186: | Rad 1 218: | ||
this.myRectangle(x0, y0 + 2 * distance, bredd, hojd, "yellow"); | this.myRectangle(x0, y0 + 2 * distance, bredd, hojd, "yellow"); | ||
this.myRectangle(x0, y0 + 3 * distance, bredd, hojd, "pink"); | this.myRectangle(x0, y0 + 3 * distance, bredd, hojd, "pink"); | ||
// välj färg att rita med | // välj färg att rita med | ||
window.addEventListener('keydown',this.check,false); | |||
if ( | // function check(e) { | ||
if ( | // alert(e.keyCode); | ||
//} | |||
if ( | function check(e) { | ||
if ( | var code = e.keyCode; | ||
if (code == 37) | |||
color = "yellow"; | |||
if (code == 38) | |||
color = "green"; //Up arrow pressed | |||
if (code == 39) | |||
color = "blue"; | |||
if (code == 40) | |||
color = "red"; | |||
} | } | ||
</pre> | </pre> | ||
}} | }} | ||
<headertabs /> | <headertabs /> |