Newbie here, currently learning how to code. My teacher gave me the task of proving myself in canvas by making a smiley face on my own. This is the result so far.
Now he gave me the task of randomizing the coordinates, so that the smiley as a whole appears in a new place everytime (whether that happens on load or through a button is my decision).
Edit: to clarify, my problem lies more within the logic behind altering the coordinates of my canvas. I've tried to figure out how to do this, and know that I'll need Math.random
for that.
Edit: to clarify, my problem lies more within the logic behind
My code:
<body onload="draw();">
<canvas id="canvas" width="150" height="150"></canvas>
<script type="application/javascript">
function draw() {
var canvas = document.getElementById("canvas");
var smiley = canvas.getContext("2d");
//Face shape
smiley.beginPath();
smiley.arc(75, 75, 50, 0, Math.PI * 2, true);
//Right eye
smiley.moveTo(65,55);
smiley.arc(55,55,10,0, Math.PI * 2, true);
//Left eye
smiley.moveTo(105,55);
smiley.arc(95,55,10,0, Math.PI * 2, true);
//Mouth
smiley.moveTo(35,75);
smiley.arc(75, 75, 40, 0, Math.PI, false);
smiley.moveTo(36,81);
smiley.lineTo(114,81);
smiley.stroke();
//Pupils
smiley.beginPath(58,55);
smiley.moveTo(58,55);
smiley.arc(55,55,3,0, Math.PI*2,true);
smiley.moveTo(98,55);
smiley.arc(95,55,3,0, Math.PI*2, true);
smiley.fill()
//Hair
smiley.beginPath(75,25);
smiley.moveTo(75,25);
smiley.lineTo(75,5);
smiley.moveTo(79,5);
smiley.lineTo(78,25);
smiley.moveTo(72,25);
smiley.lineTo(71,5);
smiley.stroke();
//Nose
smiley.beginPath();
smiley.moveTo(73,50);
smiley.lineTo(67,67);
smiley.lineTo(80,70);
smiley.stroke();
smiley.beginPath()
smiley.moveTo(73,50);
smiley.lineTo(63,69);
smiley.lineTo(80,70);
smiley.lineTo(67,67);
smiley.lineTo(73,50);
smiley.fill();
smiley.endPath();
}
</script>
</body>
Thank you for your help.