DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How Agile Works at Tesla [Video]
  • Integrating AWS With Salesforce Using Terraform
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices

Trending

  • How Agile Works at Tesla [Video]
  • Integrating AWS With Salesforce Using Terraform
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
  1. DZone
  2. Coding
  3. Languages
  4. Using the HTML5 canvas clip method to hide certain content

Using the HTML5 canvas clip method to hide certain content

Robert Nyman user avatar by
Robert Nyman
·
Nov. 09, 11 · Interview
Like (0)
Save
Tweet
Share
5.48K Views

Join the DZone community and get the full member experience.

Join For Free

people are creating amazing things with html5 canvas, especially combined with other html5 features. i thought i’d touch on a handy method that some people don’t seem to know about: canvas clip.

when it comes to presenting things on a canvas, you can stage parts of it off the canvas area or load it somewhere else and then display it at will on the canvas. another way of doing it, and also to easily create some interesting features is the clip method.

basically, what clip does is that it offers a way to display what you want on a canvas through using any shape of your liking, and then calling clip, thus hiding the other parts of the canvas.

i’ve put together a little canvas clip demo in my playground , which looks like this:

a picture of a looking glass with red and white shapes in it

the magic in it is creating a number of shapes and then calling the clip method, hiding everything that is positioned outside those areas. breaking down the code, this is what it looks like:

(function() {
    var cvs = document.getelementbyid("canvas-clip"),
        ctx = cvs.getcontext("2d");

        // create circle
        ctx.strokestyle = "transparent";
        ctx.arc(300, 100, 75, 0, math.pi*2, false);

        // create bottom shape of the looking glass
        ctx.strokestyle = "#000";
        ctx.linewidth = "10";
        ctx.moveto(350, 50);
        ctx.lineto(100, 300);
        ctx.closepath();
        ctx.stroke();
        ctx.fill();

        // clip the view of the canvas
        ctx.clip();

        // create rectangles that will shine through the clip
        ctx.fillstyle = "#fff";
        ctx.fillrect(200, 0, 200, 200);
        ctx.fillstyle = "#f00";
        ctx.fillrect(200, 0, 100, 100);
        ctx.fillrect(300, 100, 100, 100);
})();

there you have it – another little trick up your sleeve. :-)


source: http://robertnyman.com/2011/07/26/using-the-html5-canvas-clip-method-to-hide-certain-content

Clip (compiler) HTML

Opinions expressed by DZone contributors are their own.

Trending

  • How Agile Works at Tesla [Video]
  • Integrating AWS With Salesforce Using Terraform
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: