Giter Club home page Giter Club logo

two-js-demo's Introduction

General Assembly Logo

two-js Demo

Prerequisites

  • JavaScript
  • Preschool

Objectives

By the end of this, developers should be able to:

  • Draw simple 2D shapes using two.js
  • Animate simple 2D shapes using two.js

Preparation

  1. Fork and clone this repository.

two.js

Today we're going to take a look at a two.js, a two-dimensional drawing API. This demonstration is a modified version basic usage section of the two.js website.

Demo: two.js

Let's see how we can use two.js to draw a not particularly pretty circle.

Include the two.js script and your circle.js script and create a div element to hold the instance of two.

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>two.js demo</title>
  <script src="./scripts/js/lib/two.js"></script>
  <script src="./scripts/js/circle.js" defer></script>
</head>

<body>
  <div id="two-js"></div>
</body>

</html>

Instantiate the two object.

let element = document.getElementById('two-js');
let dimensions = {width: 600, height: 600};
let two = new Two(dimensions)

two.appendTo(element);

Now we'll draw a circle with a radius of 75 px in the upper left third of the two instance.

let xPosition = two.width / 3;
let yPosition = two.height / 3;
let circle = two.makeCircle(xPosition, yPosition, 75);

Now we can style the circle.

circle.fill = 'green';
circle.stroke = 'blue';
circle.linewidth = 10;

Finally, we can draw the element.

two.update();

Exercise: more shapes and animation

Now let's draw another object and animate it.

Lab: more complex shapes

Draw a snow person.

Bonus: make the snow person wave using object groupings and translations.

Additional Resources

Interested in checking out other frameworks or maybe even making some 3D things?

  • threeJS
  • babylonJS - a JavaScript WebGL framework that seems to have more tutorials and demonstrations than threeJS to help you get started. The bablyonJS framework also includes integrated physics, whereas the three.js framework does not.
  • List of WebGL Frameworks on Wikipedia

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

two-js-demo's People

Contributors

cuprous avatar

Watchers

James Cloos avatar Nicolas Garcia 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.