JS+canvas画一个圆锥实例代码


Posted in Javascript onDecember 13, 2017

以下是我们给大家分享是实例代码:

<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>

这是三水点靠木测试后的完成图:

JS+canvas画一个圆锥实例代码

Javascript 相关文章推荐
xml 与javascript结合的问题解决方法
Mar 24 Javascript
使用jQuery模板来展现json数据的代码
Oct 22 Javascript
js复制网页内容并兼容各主流浏览器的代码
Dec 17 Javascript
JQuery操作元素的css样式
Mar 09 Javascript
jQuery异步上传文件插件ajaxFileUpload详细介绍
May 19 Javascript
纯javascript实现的小游戏《Flappy Pig》实例
Jul 27 Javascript
JavaScript将DOM事件处理程序封装为event.js 出现的低级错误问题
Aug 03 Javascript
jquery代码规范让代码越来越好看
Feb 03 Javascript
React Native之prop-types进行属性确认详解
Dec 19 Javascript
React之PureComponent的使用作用
Jul 10 Javascript
Node.js系列之安装配置与基本使用(1)
Aug 30 Javascript
原生JavaScript之es6中Class的用法分析
Feb 23 Javascript
JS排序算法之冒泡排序,选择排序与插入排序实例分析
Dec 13 #Javascript
实例分析js事件循环机制
Dec 13 #Javascript
javascript实现QQ空间相册展示源码
Dec 12 #Javascript
自定义PC微信扫码登录样式写法
Dec 12 #Javascript
基于模板引擎Jade的应用(详解)
Dec 12 #Javascript
jquery获取transform里的值实现方法
Dec 12 #jQuery
JS排序算法之希尔排序与快速排序实现方法
Dec 12 #Javascript
You might like
一周学会PHP(视频)Http下载
2006/12/12 PHP
PHP安全防范技巧分享
2011/11/03 PHP
php实现与erlang的二进制通讯实例解析
2014/07/23 PHP
异步加载技术实现当滚动条到最底部的瀑布流效果
2014/09/16 PHP
Yii2.0 模态弹出框+ajax提交表单
2016/05/22 PHP
JS+CSS实现的蓝色table选项卡效果
2015/10/08 Javascript
JS中对Cookie的操作详解
2016/08/05 Javascript
JavaScript变量作用域_动力节点Java学院整理
2017/06/27 Javascript
vue的全局提示框组件实例代码
2018/02/26 Javascript
微信小程序wx:for循环的实例详解
2018/10/07 Javascript
[06:16]DOTA2守卫传承者——职业选手谈心路历程
2015/02/26 DOTA
python BeautifulSoup使用方法详解
2013/11/21 Python
Python运行报错UnicodeDecodeError的解决方法
2016/06/07 Python
Win8下python3.5.1安装教程
2020/07/29 Python
python实现根据文件关键字进行切分为多个文件的示例
2018/12/10 Python
解决Django no such table: django_session的问题
2020/04/07 Python
python基于exchange函数发送邮件过程详解
2020/11/06 Python
Jmeter调用Python脚本实现参数互相传递的实现
2021/01/22 Python
CSS3转换功能transform主要属性值分析及实现分享
2012/05/06 HTML / CSS
浅谈关于html5中图片抛物线运动的一些心得
2018/01/09 HTML / CSS
韩国江南富人区高端时尚百货商场:Galleria(格乐丽雅)
2018/03/27 全球购物
售后服务科岗位职责范文
2013/11/13 职场文书
培训演讲稿范文
2014/01/12 职场文书
浙江文明网签名寄语
2014/01/18 职场文书
《望庐山瀑布》教学反思
2014/04/22 职场文书
音乐之声音乐广播稿
2014/09/10 职场文书
甲乙双方合作协议书
2014/10/13 职场文书
诚信承诺书
2015/01/19 职场文书
医德医风个人总结
2015/02/28 职场文书
员工辞职信范文大全
2015/05/12 职场文书
2015年学校教科室工作总结
2015/07/20 职场文书
房屋转让协议书(标准范本)
2016/03/21 职场文书
施工安全责任协议书
2016/03/23 职场文书
《鲁班学艺》读后感3篇
2019/11/27 职场文书
详解Nginx的超时keeplive_timeout配置步骤
2022/05/25 Servers
html中两种获取标签内的值的方法
2022/06/10 HTML / CSS