Git Product home page Git Product logo

canvas's Introduction

Introduction to Canvas

Many students end up using Canvas for later projects. This exercise explores how Canvas works at a basic level. It will give you an introduction to using canvas to draw shapes on a page.

Phase I: Setting up your Canvas Element

Take a look at the index.html that has been provided for you. Notice that there is a canvas element that has been included in the body. This is the element that we will be working with. By default, the canvas element has a width of 300px and a height of 150px.

In the practice.js file, we have added an event listener that waits for the document to be loaded before looking for the canvas element.

Create a 2D rendering context:

  1. Grab the canvas element by id and save it to a variable
  2. Set the height and width attributes of the canvas to 500
  3. Call getContext on the canvas with '2d' as an argument and save the return value as ctx
  • You will be setting attributes and calling methods on ctx for the rest of this exercise

Phase II: Draw a Rectangle

  1. Pick a color (e.g. 'red', rgb(255,255,255), etc. ) to set as the fillStyle attribute for ctx
  2. Call fillRect with the appropriate dimensions to draw the rectangle on the canvas
  3. Check that everything's working by executing open index.html in your terminal

Phase III: Draw a Circle

To draw the circle:

  1. Invoke beginPath with no arguments
  2. Invoke arc with the appropriate dimensions to draw a circle
  3. Pick a color to set as strokeStyle
  4. Set the lineWidth attribute to some integer
  5. Invoke stroke with no arguments
  6. Open index.html in your browser to view it

To fill in the circle:

  1. Pick a color to set as fillStyle
  2. Invoke fill with no arguments
  3. Open index.html in your browser to view it

Phase IV: Draw [your Favorite Shape]

See here for inspiration. Also review these examples of how to animate your drawings.

canvas's People

Contributors

kahaan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.