Table of Contents
回复讨论(解决方案)
Home Web Front-end HTML Tutorial Draw titles and shadows in html5 canvas_html/css_WEB-ITnose

Draw titles and shadows in html5 canvas_html/css_WEB-ITnose

Jun 24, 2016 pm 12:16 PM

<!DOCTYPE html>
<html>
 <head>
   <script>
    
     function createCanopyPath(context)
     { 
        context.beginPath();
        
        context.moveTo(-25,-50);
        context.lineTo(-10,-80);
        context.lineTo(-20,-80);
        context.lineTo(-5,-110);
        context.lineTo(-15,-110);

        context.lineTo(0,-140);

        context.lineTo(15,-110);
        context.lineTo(5,-110);
        context.lineTo(20,-80);
        context.lineTo(10,-80);
        context.lineTo(25,-50);
    
        context.closePath();
     }
    
     function drawTrails()
     {
        var canvas=document.getElementById('diagonal');
        var context=canvas.getContext('2d');
        
        context.save();
        context.translate(130,250);
        //绘制树冠函数
        createCanopyPath(context);
        //填充树冠颜色
        context.lineWidth=4;
        context.lineJoin='round';
        context.strokeStyle='#663300';
        context.fillStyle='#339900';
        context.fill();

        //渐变
        var trunkGradient=context.createLinearGradient(-5,-50,5,-50);
        trunkGradient.addColorStop(0,'#663300');
        trunkGradient.addColorStop(0.4,'#996600');
        trunkGradient.addColorStop(1,'#552200');
        //树干
        //context.fillStyle='#663300';
        context.fillStyle=trunkGradient;
        context.fillRect(-5,-50,10,50);
        var canopyShadow=context.createLinearGradient(0,-50,0,0);
        canopyShadow.addColorStop(0,'rgba(0,0,0,0.5)');
        canopyShadow.addColorStop(0.2,'rgba(0,0,0,0.0)');
        context.fillStyle=canopyShadow;
        context.fillRect(-5,-50,10,50);

        context.stroke();
        context.restore();
        //路,两条曲线
        context.save();
        context.translate(-10,350);
        context.beginPath();
        context.moveTo(0,0);
        context.quadraticCurveTo(170,-50,260,-190);
        context.quadraticCurveTo(310,-250,410,-250);
        context.strokeStyle='#663300';
        context.lineWidth=20;
        context.stroke();
        context.restore();
         //阴影!!!!
        //context.save();
        //context.transform(1,0,-0.5,1,0,0);
        //context.scale(1,0.6);
        //context.fillStyle='rgba(0,0,0,0.2)';
        //context.fillRect(-5,-10,10,50);
        //createCanopyPath(context);
        //context.fill();
        //context.rotate(1.57);
        //context.drawImage(myImage,0,0,100,100);
        //context.stroke();
        //content.restore();
        //标题
        content.save();
       
        context.font="60px impact";
        context.fillStyle='#996600';
        context.textAlign='center';
        context.fillText('Happy Trails!',200,60,400);
        
        context.stroke();
        content.restore();
     }
     window.addEventListener("load",drawTrails,true);
   </script>
 </head>
 <body>
   <canvas id="diagonal" width="800px" height="800px">    
   </canvas>
 <body>
</html>
阴影也不出来 不知道是不是rotate没起作用?还有标题 不出字啊


回复讨论(解决方案)

大神们啊···

<!DOCTYPE html>
 <html>
  <head>
    <script>
     
      function createCanopyPath(context)
      { 
         context.beginPath();
         
         context.moveTo(-25,-50);
         context.lineTo(-10,-80);
         context.lineTo(-20,-80);
         context.lineTo(-5,-110);
         context.lineTo(-15,-110);
 
        context.lineTo(0,-140);
 
        context.lineTo(15,-110);
         context.lineTo(5,-110);
         context.lineTo(20,-80);
         context.lineTo(10,-80);
         context.lineTo(25,-50);
     
         context.closePath();
      }
     
      function drawTrails()
      {
         var canvas=document.getElementById('diagonal');
         var context=canvas.getContext('2d');
         
         context.save();
         context.translate(130,250);
         //绘制树冠函数
         createCanopyPath(context);
         //填充树冠颜色
         context.lineWidth=4;
         context.lineJoin='round';
         context.strokeStyle='#663300';
         context.fillStyle='#339900';
         context.fill();
 
        //渐变
         var trunkGradient=context.createLinearGradient(-5,-50,5,-50);
         trunkGradient.addColorStop(0,'#663300');
         trunkGradient.addColorStop(0.4,'#996600');
         trunkGradient.addColorStop(1,'#552200');
         //树干
         //context.fillStyle='#663300';
         context.fillStyle=trunkGradient;
         context.fillRect(-5,-50,10,50);
         var canopyShadow=context.createLinearGradient(0,-50,0,0);
         canopyShadow.addColorStop(0,'rgba(0,0,0,0.5)');
         canopyShadow.addColorStop(0.2,'rgba(0,0,0,0.0)');
         context.fillStyle=canopyShadow;
         context.fillRect(-5,-50,10,50);
 
        context.stroke();
         context.restore();
         //路,两条曲线
         context.save();
         context.translate(-10,350);
         context.beginPath();
         context.moveTo(0,0);
         context.quadraticCurveTo(170,-50,260,-190);
         context.quadraticCurveTo(310,-250,410,-250);
         context.strokeStyle='#663300';
         context.lineWidth=20;
         context.stroke();
         context.restore();
                  
                  
//         context.save();
//         context.rotate(1.57);
//         context.drawImage(myImage,0,0,100,100);
//         context.restore();
//          //阴影!!!!
//         context.save();
//         context.transform(1,0,-0.5,1,0,0);
//         context.scale(1,0.6);
//         context.fillStyle='rgba(0,0,0,0.2)';
//         context.fillRect(-5,-50,10,50);
//         createCanopyPath(context);
//         context.fill();

         //标题
         context.save();     
         context.shadowColor='rgba(0,0,0,0.2)';
         context.shadowOffsetX=15;
         context.shadowOffsetY=-10;
         context.shadowBlur=2; 
         context.font="60px impact";
         context.fillStyle='#996600';
         context.textAlign='center';
         context.fillText('Happy Trails!',200,60,400);
         context.restore();
      }
      window.addEventListener("load",drawTrails,true);
    </script>
  </head>
  <body>
    <canvas id="diagonal" width="800px" height="800px">    
    </canvas>
  <body>
 </html>
文字和阴影显示出来了

<!DOCTYPE html>
 <html>
  <head>
    <script>
     
      function createCanopyPath(context)
      { 
         context.beginPath();
         
         context.moveTo(-25,-50);
         conte…… 我试试啊·

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

What is the purpose of the <meter> element?

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

What is the purpose of the <datalist> element?

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

What is the purpose of the <progress> element?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

See all articles