|
| 1 | +// team GreatBob (Raymond Wu, Jerry Ye) |
| 2 | +// SoftDev2 pd7 |
| 3 | +// K02 -- Connecting the Dots |
| 4 | +// 2019-02-04 |
| 5 | + |
| 6 | + |
| 7 | +var canvas = document.getElementById("playground"); |
| 8 | +var ctx = canvas.getContext("2d"); |
| 9 | + |
| 10 | +var clearButton = document.getElementById("clear"); |
| 11 | + |
| 12 | +// clear canvas on button click |
| 13 | +clearButton.addEventListener( "click" , function() { |
| 14 | + |
| 15 | + ctx.clearRect( 0, 0, canvas.width, canvas.height ); |
| 16 | + console.log("canvas cleared"); |
| 17 | + |
| 18 | + previousX = previousY = undefined; //reset prev center coordinates |
| 19 | +}); |
| 20 | + |
| 21 | +var previousX; // store center coordinates of prev ellipse |
| 22 | +var previousY; |
| 23 | + |
| 24 | +// draw dot upon click on canvas |
| 25 | +canvas.addEventListener( "click" , function(e) { |
| 26 | + |
| 27 | + e.preventDefault(); // won't register a "click" |
| 28 | + |
| 29 | + ctx.beginPath(); // start ellipse path |
| 30 | + ctx.ellipse( e.offsetX, e.offsetY, 25, 25, 0, 0, 2 * Math.PI ); |
| 31 | + // x, y, radiusX, radiusY, rotation, startAngle, endAngle [, anticlockwise] |
| 32 | + ctx.strokeStyle = "#0000ff"; // blue |
| 33 | + ctx.stroke(); |
| 34 | + |
| 35 | + if (previousX != undefined) { // if not first circle |
| 36 | + ctx.beginPath(); // start line path |
| 37 | + ctx.moveTo(e.offsetX, e.offsetY); // move to center of circle |
| 38 | + ctx.lineTo( previousX, previousY ); // draw line to prev center |
| 39 | + ctx.strokeStyle = "#ff0000"; // red |
| 40 | + ctx.stroke(); |
| 41 | + } |
| 42 | + |
| 43 | + previousX = e.offsetX; // update prev center coordinates |
| 44 | + previousY = e.offsetY; |
| 45 | + |
| 46 | +}); |
| 47 | + |
0 commit comments