Clip

In this chapter you will learn:

  1. Creating a Clipping Region

How to clip a region

The clip method:

NameDescriptionReturns
clip()Creates a new clipping regionvoid

The following code clips a region.

<!DOCTYPE HTML><!-- jav a  2s .c om-->
<html>
<head>
<style>
canvas {
      border: thin solid black
}

</style>
</head>
<body>
      <canvas id="canvas" width="500" height="140"> 
      Your browser doesn't support the <code>canvas</code> element 
      </canvas>
      <script>
            var ctx = document.getElementById("canvas").getContext("2d");
            ctx.fillStyle = "yellow";
            ctx.beginPath();
            ctx.rect(20, 20, 500, 140);
            ctx.fill();
            ctx.beginPath();
            ctx.rect(100, 20, 300, 100);
            ctx.clip();
            ctx.fillStyle = "red";
            ctx.beginPath();
            ctx.rect(0, 0, 500, 140);
            ctx.fill();
      </script>
</body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. How to draw text on canvas
Home » Javascript Tutorial » Canvas
Canvas tag
Canvas Context
Rectangle
Rectangle clear
Drawing State
Drawing state restore
Line width
Line Join Style
Line cap settings
Gradients
Linear Gradient
Radial Gradient
Path
Arcs
Two types of curve
Quadratic Bezier Curves
Cubic Bezier Curves
Clip
Text
Shadow
Transparency
Stoke style