This article will explain how to use the canvas function in HTML to draw a cone-shaped graphic example with JS. canvas is newly added in HTML5 New attribute, friends who are interested in canvas in HTML5 can learn and test it.
The following is the example code we share with you:<html> <head> <title>我的第一个 HTML 页面</title> </head> <body> <canvas id='cvs' width='1000' height="800"> </canvas> <script> const cvs =document.getElementById('cvs'); // 计算画布的宽度 const width = cvs.offsetWidth; // 计算画布的高度 const height = cvs.offsetHeight; const ctx = cvs.getContext('2d'); // 设置宽高 cvs.width = width; cvs.height = height; /** ctx:context x,y:偏移 纵横坐标 w:度 h:高 color:颜色 数组,可以把颜色提取出来方便自定义 */ var Cone = function(ctx,x,y,w,h,d,color){ ctx.save(); ctx.translate(x, y); var blockHeight=h; // 中点 var x2 = 0; var y2 = 20; var k2 = 10; var w2 = w; var h2 = h; // 递减度 var decrease = d; ctx.beginPath(); ctx.moveTo(x2+w2,y2); // 椭圆加了边框,所以加1减1 ctx.bezierCurveTo(x2+w2, y2+k2, x2-w2, y2+k2, x2-w2-1, y2); ctx.lineTo(x2-w2+decrease,y2+blockHeight); ctx.bezierCurveTo(x2-w2+decrease, y2+blockHeight+k2, x2+w2-decrease, y2+blockHeight+k2, x2+w2-decrease, y2+blockHeight); ctx.lineTo(x2+w2+1,y2); var linear = ctx.createLinearGradient(x2-w2, y2,x2+w2-decrease, y2+blockHeight); linear.addColorStop(0,color[0]); linear.addColorStop(1,color[1]); ctx.fillStyle = linear ; ctx.strokeStyle=linear ctx.fill(); //ctx.stroke(); ctx.closePath(); //画椭圆 ctx.beginPath(); ctx.moveTo(x2-w2, y2); ctx.bezierCurveTo(x2-w2, y2-k2, x2+w2, y2-k2, x2+w2, y2); ctx.bezierCurveTo(x2+w2, y2+k2, x2-w2, y2+k2, x2-w2, y2); var linear = ctx.createLinearGradient(x2-w2, y2,x2+w2-decrease, y2+blockHeight); linear.addColorStop(1,color[0]); linear.addColorStop(0,color[1]); ctx.fillStyle = linear ; ctx.strokeStyle=linear ctx.closePath(); ctx.fill(); ctx.stroke(); ctx.restore(); }; function dr(m){ const colorList =[ { color:['#76e3ff','#2895ea'], height:60 }, { color:['#17ead9','#5d7ce9'], height:30 }, { color:['#1ca5e5','#d381ff'], height:40 }, { color:['#ffa867','#ff599e'], height:70 }, { color:['#ffa6e3','#ec6a70'], height:50 }, { color:['#f9c298','#d9436a'], height:30 }, { color:['#eb767b','#9971dc'], height:30 }, { color:['#f06af9','#5983ff'], height:100 }, ]; const space = 20; let coneHeight = 0; // 间隔 const gap = 20; const x = 380; const y = 20; const angle = 30; let coneWidth=0; colorList.forEach((item)=>{ coneHeight += item.height +space; }); // 最下面的先画(层级) colorList.reverse().forEach((item,index)=>{ const decrease =Math.tan(Math.PI*angle/180)*(item.height+space); coneWidth=coneWidth + decrease; coneHeight = coneHeight-item.height - space; //圆锥 Cone(ctx,x,coneHeight ,coneWidth ,item.height,decrease,item.color); // 中点 const cX =parseInt(x)+0.5; const cY = parseInt(coneHeight + space+ item.height/2)+0.5; //文字 ctx.save(); ctx.translate(cX , cY ); ctx.textBaseline='top'; ctx.textAlign='center'; const fontSize = item.height/2>=40?40:item.height/2; ctx.font = fontSize + 'px serif'; //const textMetrics = ctx.measureText('Hello World'); ctx.fillStyle = '#ffffff'; ctx.fillText('5000',0,-fontSize/3); ctx.restore(); const xLineA =parseInt(coneWidth-decrease/2)+10; const xLine =parseInt(m+xLineA )>=x?x:m+xLineA ; //线 ctx.save(); ctx.translate(cX , cY ); ctx.setLineDash([3,2]); ctx.strokeStyle = '#a4a4a4'; ctx.beginPath(); ctx.moveTo(xLineA , 0); ctx.lineTo(xLine +20, 0); ctx.stroke(); ctx.restore(); //描述文字 ctx.save(); ctx.translate(cX , cY ); ctx.textBaseline='middle'; ctx.textAlign='left'; ctx.font = '22px serif'; //const textMetrics = ctx.measureText('Hello World'); ctx.fillStyle = '#4a4a4a'; ctx.fillText('进入收件列表2',xLine+gap ,0); ctx.restore(); //转化率文字 ctx.save(); ctx.translate(cX , cY ); ctx.textBaseline='middle'; ctx.textAlign='left'; ctx.font = '28px bold serif '; ctx.fillStyle = '#007dfd'; ctx.fillText('20%',xLine+gap ,(item.height+gap)/2 ); ctx.restore(); }); } let m=0; let gravity =10; (function drawFrame(){ window.requestAnimationFrame(drawFrame,cvs); ctx.clearRect(0,0,cvs.width,cvs.height); m += gravity; dr(m); })(); </script> </body> </html>
Related recommendations:
JavaScript html5 canvas implements drawing hyperlinks on images
JavaScript canvas implements rotation animation
The above is the detailed content of JS+canvas draws a cone example code_javascript skills. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章整理了20+Vue面试题分享给大家,同时附上答案解析。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
