首页 技术 正文
技术 2022年11月18日
0 收藏 533 点赞 4,775 浏览 2128 个字

前端绘制立体效果的三角形的demo在移动端使用时,需要自适应屏幕。canvas上无法设置rem,所以在canvas外加一个父级元素设置为rem,再将canvas的宽高设置为100% 100%。如果想出现立体效果,应该将一个三角形分为三个三角形来进行绘制。突出立体的效果,应该设置过渡色。  toTriangle(a,b,c){//a为左边三角的长度,b为顶点距离中心的长度,c为右边顶点距离中心的长度var bg = document.getElementById(‘circle_triangle’);var ctx = bg.getContext(‘2d’); //自适应cavas画布的宽高,动态设置。let canvas_outer = document.getElementById(‘outer’);console.log(canvas_outer.offsetHeight);bg.height = canvas_outer.offsetHeight;bg.width = canvas_outer.offsetWidth; let bod = bg.getBoundingClientRect()let wid = bod.width;//三角形三个顶点从顶点:a_dot,左边点:b_dot,右边点:c_dot。和参数a,b,c有区别let a_dot = [wid/2,(wid/2)*(1-b)]let b_dot = [(wid/2)*(1-a*Math.cos((Math.PI)*45/180)),(wid/2)*(a*Math.sin((Math.PI)*45/180)+1)]let c_dot = [(wid/2)*(1+c*Math.cos((Math.PI)*45/180)),(wid/2)*(c*Math.sin((Math.PI)*45/180)+1)] //绘制三角形1(左边)ctx.beginPath();ctx.moveTo(wid/2,wid/2); //从A开始ctx.lineTo(b_dot[0],b_dot[1])//ctx.lineTo(a_dot[0],a_dot[1])var grd1 = ctx.createLinearGradient(b_dot[0],b_dot[1],a_dot[0],a_dot[1],);//使用渐变颜色填充,从 点b到agrd1.addColorStop(0,”#”); //起始颜色grd1.addColorStop(0.25,”#”); //起始颜色grd1.addColorStop(0.5,”#”); //起始颜色grd1.addColorStop(0.75,”#”); //终点颜色grd1.addColorStop(1,”#”); //终点颜色ctx.fillStyle=grd1; //以上面定义的渐变填充ctx.fill(); //闭合形状并且以填充方式绘制出来ctx.closePath() //绘制三角形2(右边)ctx.beginPath();ctx.moveTo(wid/2,wid/2); //从A开始ctx.lineTo(a_dot[0],a_dot[1])ctx.lineTo(c_dot[0],c_dot[1])var grd2 = ctx.createLinearGradient(a_dot[0],a_dot[1],c_dot[0],c_dot[1]);//使用渐变颜色填充,从(0,0)到(200,0) (左到右)grd2.addColorStop(0,”#”); //起始颜色grd2.addColorStop(0.25,”#”); //起始颜色grd2.addColorStop(0.5,”#”); //起始颜色grd2.addColorStop(0.75,”#”); //终点颜色grd2.addColorStop(1,”#”); //终点颜色ctx.fillStyle=grd2; //以上面定义的渐变填充ctx.fill(); //闭合形状并且以填充方式绘制出来ctx.closePath() //绘制三角形3(底部)ctx.beginPath();ctx.moveTo(wid/2,wid/2); //从A开始ctx.lineTo(b_dot[0],b_dot[1])//ctx.lineTo(c_dot[0],c_dot[1]);var grd3 = ctx.createLinearGradient(b_dot[0],b_dot[1],c_dot[0],c_dot[1]);//使用渐变颜色填充,从(0,0)到(200,0) (左到右)grd3.addColorStop(0,”#”); //起始颜色grd3.addColorStop(0.25,”#”); //起始颜色grd3.addColorStop(0.5,”#”); //起始颜色grd3.addColorStop(0.75,”#”); //终点颜色grd3.addColorStop(1,”#”); //终点颜色ctx.fillStyle=grd3; //以上面定义的渐变填充ctx.fill(); //闭合形状并且以填充方式绘制出来ctx.closePath()}   

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898