Giter VIP home page Giter VIP logo

irregular-shape-rollover's Introduction

Irregular shape rollover

This is a quick demo showing how you can use the HTML5 Canvas API to create a rollover for irregular shapes.

You can try out the demo at http://codepo8.github.io/irregular-shape-rollover/

Rollover demo

The usage of this example is very simple. Just include the shaperollover.js script and add a class of rollover to each image you want to add a roll-over effect to.

These images should be PNG or GIF images with transparent and non-transparent parts. Every time your mouse cursor is over a non-transparent pixel the image gets a class of over added to it. When the cursor is on a transparent pixel the class gets removed.

Under the hood what happens is that when you mouse over an image with a class of rollover the image gets copied into a Canvas element and moving the mouse then reads the colour of the pixel at the mouse position. If that pixel has an alpha of zero, you outside the shape, if it doesn't, you are in it.

If you want to embed the images into a link and make sure that only the areas inside the rollover will be clickable take a look at the demo inlined in index.html. The trick is to add an event listener to the link and check if the image inside it has a class of over when the click happens. If it doesn't, simply call preventDefault() which stops the link from being followed.

irregular-shape-rollover's People

Contributors

codepo8 avatar

Watchers

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