Make a little mouse-interactive SVG graphic with some JavaScript.

Go fullscreen: ⌃⌥F

SVG ♥ JS

What & why

To make really fun & interesting visual interactions SVG can be very helpful.

SVG is best when combined with HTML, CSS & JavaScript—together the foursome can create magical things.

Understanding how to manipulate SVG elements with JavaScript can make great interactions.

Moving dinosaur egg

Set up

  1. This is an individual coding activity
  2. Fork & clone this repo
  3. Download the assets
  4. We’re going to work together, but independently, to each create an interactive SVG

The process:

We’ll walk through each question together and come up with a solution for each individual carousels.

  1. What HTML & CSS are necessary?
  2. What should the JavaScript do?
  3. Make the JS do its thing!

What HTML & CSS are necessary?

  • How do we export the SVG?
  • How do we insert the SVG into the HTML?
  • How do we control the egg’s position?
  • How do we animate the egg from position to position?

Code out the necessary HTML & CSS.

1

What should the JavaScript do?

  • When does the egg move from side-to-side?
  • What manipulations to the HTML need to be made?
  • What manipulations to the CSS need to be made?

Write, in English, what the JS is doing.

2

Make the JS do its thing!

  • What order does the code need to be in?
  • What elements in the HTML need to be selected?
  • What CSS classes need to be manipulated?

Write, in a new JS file, the necessary code.

3
jQuery
// Select an HTML tag; using CSS selectors
$('.dino')

  // Add a class to an element
  .addClass('move')
  // Remove a class from an element
  .removeClass('move')

  // Calculate the width of an element
  .outerWidth()
jQuery
// When the mouse is moved on an element
$('.dino').on('mousemove', function (eventObj) {

  // The current x coordinate of the mouse on the element
  eventObj.clientX

});

// Code branching: if-statement
if (location < 10) {
  // True path
} else {
  // False path
}
Start

Make a little mouse-interactive SVG graphic with some JavaScript.