Drawing Project Guide
View the DemoDay 1
Morning Session
-
If you haven't already done so, install
Node.js and
yarn (
npm install --global yarn). -
Download the
starter code. After unzipping, run
yarnto install dependencies. -
Create an HTML, CSS, and TypeScript (
src/index.ts) file. Connect these files together so that aconsole.logdisplays in the JavaScript console. Runyarn devto compile your TypeScript and start a local server. - In your HTML, add a canvas element. Then, write a TypeScript function to return that element.
- Add radio buttons for the different tools: rectangle, circle, and pencil. Write a function to read the currently selected tool from the DOM.
- Add 2 input elements of type color: one for stroke color and one for fill color. For both input elements, whenever the input value changes, store the updated value in a TypeScript variable. (It's fine if you don't finish, these aren't essential)
Afternoon Session
- Define types for
PointandRect. -
Define a
Shapeinterface. DefineRectangleShape,EllipseShape, andPolylineShape— you can choose whether to usetype,interface, orclassfor these, but they should all conform toShape. -
Hardcode an example rectangle, ellipse, and polyline in a new array
of
Shape. -
Write a
Drawingclass. This class should have adrawmethod or property that draws an array of shapes onto the canvas. Start by drawing the hardcoded array.
Day 2
Morning Session
- Download the zip from slack 🙈
Afternoon Session
- Implement the ellipse tool
-
Implement the pencil tool. Consider using an array of
Pointobjects to store the line as it's drawn. Use a thecanvasmethodsbeginPath,moveTo, andlineTo. -
Handle touch events in addition to mouse events.
There are analogous touch events for all the mouse events we use:
touchstart,touchmove,touchend.You can access touch coordinates using, e.g.:
event.changedTouches[0].clientX
See also: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events -
Cancel the drag if the user releases the mouse outside the canvas
element. Consider adding a
mouseupevent listener ondocumentto do this.