UI Design Patterns – Canvas

A blog about UI design patterns and how they can help you with your programming.

To start, let’s explore the idea of a pattern by looking at some of the most common ones for web applications.

MVC Pattern

The Model-View-Controller (MVC) pattern is one of the oldest design patterns in software architecture. It was first created back in the 1970s, and has been widely used ever since. The MVC pattern provides a way to cleanly separate code into three parts: the model, the view and the controller.

Model – this is where data is stored and processed.

View – this is how that data is presented to users.

Controller – this connects everything together, responding to user actions and updating the view whenever the model changes.*

The Canvas UI design pattern is a great way to implement freeform drawing within your web page or web application.

The HTML5 canvas element provides a surface on which you can dynamically draw using JavaScript. It is ideal for building games, drawing applications, and pretty much anything else that involves animating objects on screen.

The Canvas pattern provides a set of functions that makes it easier to work with the canvas element. These functions are the following:

create – creates the canvas element

getContext – returns the drawing context object associated with the canvas element

clear – clears the canvas of any drawn content

setSize – specifies the size of the canvas using css width and height properties (the css can be overwritten by providing an explicit width and/or height as attributes on the canvas element)

getSize – returns an object literal containing the width and height properties of the canvas

setPixelRatio – specifies a pixel ratio for high resolution devices like retina displays; this will make your drawings appear sharp if you choose a ratio other than 1

getPixelRatio – returns the pixel ratio used by the canvas element; this will be 1 unless you have set it explicitly, or unless you are viewing your code on a high-resolution display (and in






The Canvas is the area that all UI (User Interface) Allows a user to interact with your application. More info

See in Glossary elements should be inside. The Canvas Scaler component can be used to scale the size of the UI if the screen resolution is larger or smaller than the reference resolution. The Canvas functionality is provided by UnityEngine.UI at runtime, and can also be used in your own custom code by extending MonoBehaviour.

The Canvas is not a visible object itself; it is an abstract component for arranging UI elements in the sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info

See in Glossary. It contains several components that allow you to position UI elements in different ways:

Transform – Defines the position, rotation and scale of the UI element inside the Canvas. This component has no effect on its own, but other components such as Rect Transform (the default UI element component) use it to determine how to render itself on screen.

Rect Transform – By default, all UI elements have this component attached, which determines

You are building an online painting application. The user can choose a color and draw on your canvas element. You want to give the user a preview of the color they have chosen so you decide to add a 2 pixel by 2 pixel div element to your page with the background color set to the chosen color. You want this color preview element to move around with the mouse pointer when the user is deciding what color to use for their next stroke.

You can accomplish this by getting the x and y coordinates of the mouse pointer and setting the left and top style properties of your preview element to those coordinates. This code could look like this:

I’ve been working on a project that involves some heavy canvas usage. I learned a lot of things while working on these features, so I wanted to share some of my thoughts.


1. Canvas vs DOM

2. Rendering order

3. How to make fast canvas apps

4. Canvas state machine

5. Conclusion

The first thing to know about canvas is that it’s fundamentally different from DOM elements and all other HTML elements you have used before, because it doesn’t have any state except raw pixels and a few attributes like width, height and globalCompositeOperation, which are also accessible from JavaScript. That’s why canvas is fast – it just draws pixels – but it’s hard to use for UI design, because everything you draw on the canvas is nothing more than pixels, so there are no mouse events or anything like that! Every application built with the canvas element has to implement this functionality by itself using the mouse events that are available on the page itself (mousemove, click) or even the touch events (touchstart). This means that if you want your app to work on touchscreen devices without any problems it will be harder than just drawing something on a div element! The only way out of this problem is using third

Who are we?

The Khan Academy team is a small, non-profit team based in Mountain View, California. We’re on a mission to provide a free world-class education for anyone, anywhere.

Together, we create software that enables people to learn and practice at their own pace in and outside of the classroom. Our personalized learning engine uses student data to intelligently customize practice problems for each individual user.

Our product ecosystem is built around Khan Academy’s free website (khanacademy.org), which provides over 5,000 videos and 100,000 interactive Common Core aligned math practice questions covering K-12 math, science topics such as biology, chemistry, and physics, and even reaches into the humanities with playlists on finance and history. We also have a mobile app available on iOS and Android devices.

In addition to our core product offering, we partner with other educational organizations who use our platform to deliver their own lessons and assessments to students. Our partners include the College Board (SAT prep), NPR (civic education), the Museum of Modern Art (art history), NASA (astronomy) and dozens more.

Leave a Reply

Your email address will not be published. Required fields are marked *