HTML, CSS och Javascript: Skillnad mellan sidversioner
Hoppa till navigering
Hoppa till sök
Hakan (diskussion | bidrag) |
Hakan (diskussion | bidrag) |
||
(9 mellanliggande sidversioner av samma användare visas inte) | |||
Rad 225: | Rad 225: | ||
this.myRectangle(50, 20, 50, 70, "Green"); | this.myRectangle(50, 20, 50, 70, "Green"); | ||
this.myRectangle(90, 200, 50, 70, "Red"); | this.myRectangle(90, 200, 50, 70, "Red"); | ||
</script> | |||
</body> | |||
</html> | |||
</pre> | |||
}} | |||
=== Använd variabler till funktionerna === | |||
I exemplet nedan har vi lagt till variabler för att styra var de röda rektanglarna hamnar. | |||
{{Lista | Variabler och funktioner | |||
<pre> | |||
<!DOCTYPE html> | |||
<html> | |||
<body> | |||
<canvas id="myCanvas" width="400" height="500" | |||
style="border:1px solid #d3d3d3;"> | |||
Your browser does not support the canvas element. | |||
</canvas> | |||
<script> | |||
var canvas = document.getElementById("myCanvas"); | |||
var ctx = canvas.getContext("2d"); | |||
var startX = 40; | |||
var startY = 40; | |||
var distX = 80; | |||
var distY = 110; | |||
var bredd = 50; | |||
function myRectangle(x, y, w, l, color) { | |||
ctx.fillStyle = color; | |||
ctx.fillRect(x, y, w, l); | |||
} | |||
this.myRectangle(startX, startY, bredd, 70, "Red"); | |||
this.myRectangle(startX + distX, startY, bredd, 70, "Red"); | |||
this.myRectangle(startX + 2*distX, startY, bredd, 70, "Red"); | |||
this.myRectangle(startX, startY + distY, bredd, 70, "Red"); | |||
this.myRectangle(startX + distX, startY + distY, bredd, 70, "Red"); | |||
this.myRectangle(startX + 2*distX, startY + distY, bredd, 70, "Red"); | |||
</script> | </script> | ||
Rad 984: | Rad 1 027: | ||
{{clear}} | {{clear}} | ||
== | == Andra plattformar för spelutveckling (med Javascript) == | ||
* [http://phaser.io Phaser] | |||
* [http://processingjs.org/ Processing] | |||
// | |||
= Ritprogrammet = | = Ritprogrammet = | ||
Rad 1 200: | Rad 1 119: | ||
} | } | ||
// Här | // Här börjar koden inspirerad av spelprogrammering.nu | ||
Rad 1 207: | Rad 1 126: | ||
context.fillStyle = color | context.fillStyle = color | ||
context.fillRect(x, y, w, l); | context.fillRect(x, y, w, l); | ||
} | } | ||
Rad 1 224: | Rad 1 135: | ||
} | } | ||
// 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"); | ||
Rad 1 231: | Rad 1 142: | ||
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 240: | Rad 1 151: | ||
function check(e) { | function check(e) { | ||
var code = e.keyCode | var code = e.keyCode | ||
// | // Ändra färg | ||
if (code == 71) | if (code == 71) | ||
color = "green"; | color = "green"; | ||
Rad 1 249: | Rad 1 160: | ||
if (code == 80) | if (code == 80) | ||
color = "pink"; | color = "pink"; | ||
// | // Ändra radie | ||
if (code == 49) | if (code == 49) | ||
radie = 5; | radie = 5; | ||
Rad 1 255: | Rad 1 166: | ||
radie = 10; | radie = 10; | ||
} | } | ||
</pre> | </pre> | ||
}} | }} | ||
=== Exempel === | |||
: Ett [https://19huhe.ssis.nu/ritprogram/ ritprogram] av Hugo Helm | |||
: Ett [https://19thli.ssis.nu/wp-content/uploads/2019/09/index.html ritprogram] av Theresia Lindahl. | |||
<headertabs /> | <headertabs /> |