04 Drawing
04 Drawing
▪ Drawing models
▪ SimpleKit
▪ Graphics context
▪ Drawable Object
▪ Painter’s Algorithm
▪ Display List
Drawing 1
Model-View-Controller (MVC)
present
View
perceive notify
mental system
model Model model
express
change
Controller
translate
Drawing 2
Graphical Presentation Architecture
View
Graphics
Windowing
System Model
Input
Controller
Drawing 3
Windows
Drawing 4
Windowing System
Window 1
Window 2
Application 1
Application 2
Windowing System
GPU
Drawing 5
Canvas Abstraction
(0,0)
display
(0,0)
application
(0,0)
application
Drawing 6
Window Manager
window
manager
UI
window window
manager manager
UI UI
Drawing 7
Browser as Windowing System
Drawing 8
Drawing and User Interface Toolkits
fill
button() OK
stroke
OK text
Stroke
DrawLine(x1, y1, x2, y2)
Region
DrawRect(x, y, w, h)
Drawing 10
Drawing Style
Consider DrawLine( … )
- what style options are there?
▪ Observations:
• most choices are the same for multiple
calls to DrawLine()
• lots of different parameters, but may only
want to set one or two
Drawing 11
Graphics Context
Stroke(RED)
StrokeThickness(5)
DrawLine( … )
Stroke(BLUE)
DrawLine( … )
StrokeThickness(10)
DrawLine( … )
Drawing 12
html-canvas
▪ CanvasRenderingContext2D
- Drawing commands
- Set drawing style
Drawing 13
SimpleKit
▪ We’re using SimpleKit for first part of course (incl. A1 and A2)
- Simulates a windowing system and other UI layers in browser
- Different toolkit modes (e.g. canvas-mode, imperative-mode)
- By design, it’s somewhat incomplete and very limited
- We'll examine how it's built to illustrate UI architecture
▪ The demo repo includes SimpleKit as a git submodule You’ll use SimpleKit
as an npm package
- See README for cloning and updating instructions in your assignments
Drawing 14
simplekit-canvas
Drawing 15
simplekit-canvas rectangleDemo()
Drawing 16
simplekit-canvas pathDemo()
▪ Draw line
- moveTo vs lineTo
▪ Draw circle
- Using arc
- Using ellipse
Drawing 17
simplekit-canvas pathHouseDemo()
Drawing 18
simplekit-canvas textDemo()
Drawing 19
Specifying Colour
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value Drawing 20
simplekit-canvas colourDemo()
Drawing 21
CanvasRenderingContext2D State
Drawing 22
simplekit-canvas saveState()
gc.fillStyle = "blue";
gc.strokeStyle = "red";
gc.lineWidth = 5;
circle(50, 50);
gc.fillStyle = "yellow";
gc.strokeStyle = "black";
gc.lineWidth = 2;
circle(110, 50);
circle(170, 50);
Drawing 23
simplekit-canvas fpsDemo()
Drawing 24
Drawable Objects
▪ Drawable class
▪ Display list
▪ Painter's Algorithm
Drawing 25
Drawable Object
Drawing 27
Painter’s Algorithm
Drawing 28
The 1 Minute Painting
- https://www.youtube.com/watch?v=0CFPg1m_Umg
Drawing 29
drawable paintersDemo()
Drawing 30
Strategy to Draw Complex Shapes
(0,0)
Drawing 31
Display List
Drawing 32
drawable: displayListDemo()
Drawing 33
Efficiency
Drawing 34
Resources
Drawing 36
Exercise
EXERCISE
1. Create your own SimpleKit project
- Create a new minimal Vite project, install simplekit from npm
- Import “canvas-mode” from simplekit, call startSimpleKit()
- check console for the start up message
Web Apps 37