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
yarn
to install dependencies. -
Create an HTML, CSS, and TypeScript (
src/index.ts
) file. Connect these files together so that aconsole.log
displays in the JavaScript console. Runyarn dev
to 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
Point
andRect
. -
Define a
Shape
interface. DefineRectangleShape
,EllipseShape
, andPolylineShape
— you can choose whether to usetype
,interface
, orclass
for these, but they should all conform toShape
. -
Hardcode an example rectangle, ellipse, and polyline in a new array
of
Shape
. -
Write a
Drawing
class. This class should have adraw
method 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
Point
objects to store the line as it's drawn. Use a thecanvas
methodsbeginPath
,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
mouseup
event listener ondocument
to do this.