HTML, CSS och Javascript: Skillnad mellan sidversioner
Hoppa till navigering
Hoppa till sök
Hakan (diskussion | bidrag) |
Hakan (diskussion | bidrag) |
||
Rad 1 156: | Rad 1 156: | ||
{{Lista | main.js | {{Lista | main.js | ||
<pre> | <pre> | ||
// Thanks to Jagadesha NH: | // Thanks to Jagadesha NH: | ||
// https://medium.com/@jagadeshanh/html5-canvas-click-and-draw-f665e02f5744 | // https://medium.com/@jagadeshanh/html5-canvas-click-and-draw-f665e02f5744 | ||
Rad 1 164: | Rad 1 164: | ||
var width = canvas.width = window.innerWidth; | var width = canvas.width = window.innerWidth; | ||
var mouseClicked = false, mouseReleased = true; | var mouseClicked = false, mouseReleased = true; | ||
var x0 = 50; | |||
y0 = 30; | |||
bredd =40; | |||
hojd = 40; | |||
distance = 40; | |||
color = "green"; | |||
radie = 10; | |||
size = 22; | |||
document.addEventListener("click", onMouseClick, false); | document.addEventListener("click", onMouseClick, false); | ||
document.addEventListener("mousemove", onMouseMove, false); | document.addEventListener("mousemove", onMouseMove, false); | ||
Rad 1 172: | Rad 1 181: | ||
if (mouseClicked) { | if (mouseClicked) { | ||
context.beginPath(); | context.beginPath(); | ||
context.arc(e.clientX, e.clientY, | context.arc(e.clientX, e.clientY, radie, 0, Math.PI * 2, false); | ||
context.lineWidth = 5; | context.lineWidth = 5; | ||
context.strokeStyle = color; | context.strokeStyle = color; | ||
context.stroke(); | context.stroke(); | ||
} | } | ||
//en mouse over-funktion | |||
if ((e.clientX > x0) && (e.clientX < x0 + bredd) && (e.clientY > y0) | |||
&& (e.clientY < y0 + hojd)) { color = "green"; } | |||
} | } | ||
// Här | // Här b�rjar koden inspirerad av spelprogrammering.nu | ||
function myRectangle(x, y, w, l, color) | function myRectangle(x, y, w, l, color) | ||
{ | { | ||
context.fillStyle = color | context.fillStyle = color | ||
Rad 1 195: | Rad 1 200: | ||
} | } | ||
function myCircle(x, y, r, t, color) | function myCircle(x, y, r, t, color) | ||
{ | { | ||
context.beginPath(); | context.beginPath(); | ||
Rad 1 210: | Rad 1 215: | ||
} | } | ||
// rita de | // 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"); | ||
this.myText(x0-30, y0 + distance+25, 18, "b", "blue"); | this.myText(x0-30, y0 + distance+25, 18, "b", "blue"); | ||
Rad 1 217: | Rad 1 222: | ||
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 | |||
window.addEventListener('keydown',this.check,false); | window.addEventListener('keydown',this.check,false); | ||
Rad 1 225: | Rad 1 230: | ||
function check(e) { | function check(e) { | ||
var code = e.keyCode | var code = e.keyCode | ||
// ändra färg | |||
if (code == | if (code == 71) | ||
color = "green"; | |||
if (code == 66) | |||
color = "green"; | |||
if (code == | |||
color = "blue"; | color = "blue"; | ||
if (code == | if (code == 89) | ||
color = " | color = "yellow"; | ||
if (code == 80) | |||
color = "pink"; | |||
// ändra radie | |||
if (code == 49) | |||
radie = 5; | |||
if (code == 50) | |||
radie = 10; | |||
} | } | ||
</pre> | </pre> | ||
}} | }} | ||
<headertabs /> | <headertabs /> |