Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Canvas Cookbook

You're reading from   Canvas Cookbook Over 80 simple but creative and structured recipes to explore the capabilities of HTML5 Canvas

Arrow left icon
Product type Paperback
Published in May 2019
Publisher
ISBN-13 9781785284892
Length 254 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Paths and Text FREE CHAPTER 2. Shapes and Composites 3. Animation 4. Images and Videos 5. Interactivity through Events 6. Creating Graphs and Charts 7. 3D Modeling 8. Game Development 9. Interoperability and Deployment Index

Drawing a rainbow

This is an implementation of a quadratic curve.

The output of our new recipe looks like this:

Drawing a rainbow

How to do it...

The recipe is quite simple, comprising seven calls to the function quadraticTo() to draw seven different curves.

Here is the recipe:

<html>
<head>
  <title>Rainbow</title>
  <script type="text/javascript">
    var can;
    var ctx;
    function init() {
      can = document.getElementById("MyCanvasArea");
      ctx = can.getContext("2d");
      y=can.height/2;
      x=can.width-20;
      mid=can.width/2;
      //rainbow - vibgyor
      drawQuadraticCurve(20,y,mid,0,x,y,"violet",7);
      drawQuadraticCurve(20,y-10,mid,-10,x,y-10,"indigo",7);
      drawQuadraticCurve(20,y-20,mid,-20,x,y-20,"blue",7);
      drawQuadraticCurve(20,y-30,mid,-30,x,y-30,"green",7);
      drawQuadraticCurve(20,y-40,mid,-40,x,y-40,"yellow",7);
      drawQuadraticCurve(20,y-50,mid,-50,x,y-50,"orange",7);
      drawQuadraticCurve(20,y-60,mid,-60,x,y-60,"red",7);    
      
    }
    function drawQuadraticCurve(xStart,yStart,xControl, yControl, xEnd, yEnd,color,width)
    {
      //refer the previous recipe for code
      //....
    }
  </script>
</head>
<body onload="init()">
  <canvas id="MyCanvasArea" width="800" height="400" style="border:2px solid black;" >
    browser doesn't support canvas
  </canvas>
</body>
</html>

How it works...

The drawQuadraticCurveTo() is the same function as used in the previous recipe. This function is called multiple times from the init() method.

You have been reading a chapter from
Canvas Cookbook
Published in: May 2019
Publisher:
ISBN-13: 9781785284892
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image