The example explains the method of drawing shadow effect with HTML5 canvas

Time:2021-7-23

To create a shadow effect, you need to operate on the following four attributes:

1. Context. Shadowcolor: shadow color.
2. Context. Shadowoffsetx: X-axis displacement of shadow. Positive values to the right and negative values to the left.
3. Context. ShadowOffsetY: Shadow Y-axis displacement. Positive values down, negative values up.
4. Context. Shadowblur: Shadow blur filter. The larger the data, the greater the diffusion.
If you set the first one and any of the remaining three of these four attributes, you will have a shadow effect. Usually, however, all four properties need to be set.

For example, create a red shadow that shifts 5px to the right and down, blurring 2px, which can be written like this.

JavaScript CodeCopy content to clipboard
  1. context.shadowColor = “red”;   
  2. context.shadowOffsetX = 5;   
  3. context.shadowOffsetY = 5;   
  4. context.shadowBlur= 2;  

It should be noted that the shadow here, like other attribute settings, is based on the state. Therefore, if you want to apply shadows to only one object instead of global shadows, you need to reset these four attributes of shadows before the next painting.
Results of operation:
2016325110954383.jpg (850×500)

Shadow text:

As long as the values of shadowoffsetx and shadowOffsetY are set, when the values are both positive, the shadow is relative to the bottom right of the text

Square offset. When the values are all negative, the shadow is offset relative to the upper left of the text.

3D pull effect:

Draw text repeatedly in the same position, and change shadowoffsetx, shadowOffsetY and shadowblur at the same time

From small to large, the offset increases and the transparency increases. You get the shadow effect text.

Edge blur text:

On the basis of 3D shadow effect, repeat in four directions to get the text effect of edge feathering.

Operation effect:
2016325111023538.jpg (472×470)

Program code:

JavaScript CodeCopy content to clipboard
  1. <!DOCTYPE html>     
  2. <html>     
  3. <head>     
  4. <meta http-equiv=“X-UA-Compatible” content=“chrome=IE8”>     
  5. <meta http-equiv=“Content-type” content=“text/html;charset=UTF-8”>     
  6. <title>Canvas Clip Demo</title>     
  7. <link href=“default.css” rel=“stylesheet” />     
  8.     <script>     
  9.         var ctx = null// global variable 2d context     
  10.         var imageTexture = null;     
  11.         window.onload = function() {     
  12.             var canvas = document.getElementById(“text_canvas”);     
  13.             console.log(canvas.parentNode.clientWidth);     
  14.             canvas.width = canvas.parentNode.clientWidth;     
  15.             canvas.height = canvas.parentNode.clientHeight;     
  16.                  
  17.             if (!canvas.getContext) {     
  18.                 console.log(“Canvas not supported. Please install a HTML5 compatible browser.”);     
  19.                 return;     
  20.             }     
  21.             var context = canvas.getContext(‘2d’);     
  22.                  
  23.             // section one – shadow and blur     
  24.             context.fillStyle=“black”;     
  25.             context.fillRect(0, 0, canvas.width, canvas.height/4);     
  26.             context.font = ’60pt Calibri’;     
  27.                  
  28.             context.shadowColor = “white”;     
  29.             context.shadowOffsetX = 0;     
  30.             context.shadowOffsetY = 0;     
  31.             context.shadowBlur = 20;     
  32.             context.fillText(“Blur Canvas”, 40, 80);     
  33.             context.strokeStyle = “RGBA(0, 255, 0, 1)”;     
  34.             context.lineWidth = 2;     
  35.             context.strokeText(“Blur Canvas”, 40, 80);     
  36.                  
  37.             // section two – shadow font     
  38.             var hh = canvas.height/4;     
  39.             context.fillStyle=“white”;     
  40.             context.fillRect(0, hh, canvas.width, canvas.height/4);     
  41.             context.font = ’60pt Calibri’;     
  42.                  
  43.             context.shadowColor = “RGBA(127,127,127,1)”;     
  44.             context.shadowOffsetX = 3;     
  45.             context.shadowOffsetY = 3;     
  46.             context.shadowBlur = 0;     
  47.             context.fillStyle = “RGBA(0, 0, 0, 0.8)”;     
  48.             context.fillText(“Blur Canvas”, 40, 80+hh);     
  49.                  
  50.             // section three – down shadow effect     
  51.             var hh = canvas.height/4 + hh;     
  52.             context.fillStyle=“black”;     
  53.             context.fillRect(0, hh, canvas.width, canvas.height/4);     
  54.             for(var i = 0; i < 10; i++)     
  55.             {     
  56.                 context.shadowColor = “RGBA(255, 255, 255,” + ((10-i)/10) + “)”;     
  57.                 context.shadowOffsetX = i*2;     
  58.                 context.shadowOffsetY = i*2;     
  59.                 context.shadowBlur = i*2;     
  60.                 context.fillStyle = “RGBA(127, 127, 127, 1)”;     
  61.                 context.fillText(“Blur Canvas”, 40, 80+hh);     
  62.             }     
  63.                  
  64.             // section four –  fade effect     
  65.             var hh = canvas.height/4 + hh;     
  66.             context.fillStyle=“green”;     
  67.             context.fillRect(0, hh, canvas.width, canvas.height/4);     
  68.             for(var i = 0; i < 10; i++)     
  69.             {     
  70.                 context.shadowColor = “RGBA(255, 255, 255,” + ((10-i)/10) + “)”;     
  71.                 context.shadowOffsetX = 0;     
  72.                 context.shadowOffsetY = -i*2;     
  73.                 context.shadowBlur = i*2;     
  74.                 context.fillStyle = “RGBA(127, 127, 127, 1)”;     
  75.                 context.fillText(“Blur Canvas”, 40, 80+hh);     
  76.             }     
  77.             for(var i = 0; i < 10; i++)     
  78.             {     
  79.                 context.shadowColor = “RGBA(255, 255, 255,” + ((10-i)/10) + “)”;     
  80.                 context.shadowOffsetX = 0;     
  81.                 context.shadowOffsetY = i*2;     
  82.                 context.shadowBlur = i*2;     
  83.                 context.fillStyle = “RGBA(127, 127, 127, 1)”;     
  84.                 context.fillText(“Blur Canvas”, 40, 80+hh);     
  85.             }     
  86.             for(var i = 0; i < 10; i++)     
  87.             {     
  88.                 context.shadowColor = “RGBA(255, 255, 255,” + ((10-i)/10) + “)”;     
  89.                 context.shadowOffsetX = i*2;     
  90.                 context.shadowOffsetY = 0;     
  91.                 context.shadowBlur = i*2;     
  92.                 context.fillStyle = “RGBA(127, 127, 127, 1)”;     
  93.                 context.fillText(“Blur Canvas”, 40, 80+hh);     
  94.             }     
  95.             for(var i = 0; i < 10; i++)     
  96.             {     
  97.                 context.shadowColor = “RGBA(255, 255, 255,” + ((10-i)/10) + “)”;     
  98.                 context.shadowOffsetX = -i*2;     
  99.                 context.shadowOffsetY = 0;     
  100.                 context.shadowBlur = i*2;     
  101.                 context.fillStyle = “RGBA(127, 127, 127, 1)”;     
  102.                 context.fillText(“Blur Canvas”, 40, 80+hh);     
  103.             }     
  104.         }     
  105.              
  106.     </script>     
  107. </head>     
  108. <body>     
  109.     <h1>HTML5 Canvas Clip Demo – By Gloomy Fish</h1>     
  110.     <pre>Fill And Stroke Clip</pre>     
  111.     <div id=“my_painter”>     
  112.         <canvas id=“text_canvas”></canvas>     
  113.     </div>     
  114. </body>     
  115. </html>    

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]