欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS+canvas畫(huà)一個(gè)圓錐實(shí)例代碼

 更新時(shí)間:2017年12月13日 09:18:10   投稿:laozhang  
本篇文章給大家講解html中用canvas函數(shù)配合JS畫(huà)出一個(gè)圓錐形的圖形實(shí)例,有需要的朋友學(xué)習(xí)測(cè)試下吧。

以下是我們給大家分享是實(shí)例代碼:

<html>
<head>
<title>我的第一個(gè) HTML 頁(yè)面</title>
</head>
<body>
<canvas id='cvs' width='1000' height="800">
</canvas>
<script>
const cvs =document.getElementById('cvs');

 // 計(jì)算畫(huà)布的寬度
  const width = cvs.offsetWidth;
  // 計(jì)算畫(huà)布的高度
 const  height = cvs.offsetHeight;
const ctx = cvs.getContext('2d');
  // 設(shè)置寬高
  cvs.width = width;
  cvs.height = height;
/**
ctx:context
x,y:偏移 縱橫坐標(biāo)
w:度
h:高
color:顏色 數(shù)組,可以把顏色提取出來(lái)方便自定義
*/
var Cone = function(ctx,x,y,w,h,d,color){
ctx.save();
ctx.translate(x, y);
var blockHeight=h;
// 中點(diǎn)
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();
//畫(huà)橢圓
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;
});
// 最下面的先畫(huà)(層級(jí))
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);
// 中點(diǎn)
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('進(jìn)入收件列表2',xLine+gap ,0);
ctx.restore();
//轉(zhuǎn)化率文字
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>

這是腳本之家測(cè)試后的完成圖:

相關(guān)文章

  • Bootstrap的圖片輪播示例代碼

    Bootstrap的圖片輪播示例代碼

    Bootstrap 是一個(gè)用于快速開(kāi)發(fā) Web 應(yīng)用程序和網(wǎng)站的前端框架。Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的。本文給大家分享Bootstrap的圖片輪播示例代碼,小伙伴們快來(lái)圍觀吧。
    2015-08-08
  • 使用Typescript開(kāi)發(fā)微信小程序的步驟詳解

    使用Typescript開(kāi)發(fā)微信小程序的步驟詳解

    這篇文章主要介紹了使用Typescript開(kāi)發(fā)微信小程序的步驟詳解,本文分步驟通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • require.js配合插件text.js實(shí)現(xiàn)最簡(jiǎn)單的單頁(yè)應(yīng)用程序

    require.js配合插件text.js實(shí)現(xiàn)最簡(jiǎn)單的單頁(yè)應(yīng)用程序

    這篇文章主要介紹了require.js配合插件text.js實(shí)現(xiàn)最簡(jiǎn)單的單頁(yè)應(yīng)用程序,需要的朋友可以參考下
    2016-07-07
  • JS中給數(shù)組去重的方法小結(jié)

    JS中給數(shù)組去重的方法小結(jié)

    給一個(gè)存放數(shù)字或字符串的數(shù)組去重很簡(jiǎn)單,那么現(xiàn)在問(wèn)題升級(jí),如何一個(gè)數(shù)組a里面存放的元素是若干個(gè)數(shù)組,那么如何給這個(gè)數(shù)組a去重?本文給大家介紹了JS中給數(shù)組去重的方法小結(jié),需要的朋友可以參考下
    2024-08-08
  • JavaScript XML操作 封裝類

    JavaScript XML操作 封裝類

    最近研究XML 用JavaScript寫(xiě)了一個(gè)簡(jiǎn)單的XML讀取的操作類發(fā)給大家分享一下 可兼容 IE 火狐 Safari Chrome 6月30日下午 新修改了一下
    2009-07-07
  • Javascript必知必會(huì)(四)js類型轉(zhuǎn)換

    Javascript必知必會(huì)(四)js類型轉(zhuǎn)換

    這篇文章主要介紹了Javascript必知必會(huì)(四)js類型轉(zhuǎn)換 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • js css自定義分頁(yè)效果

    js css自定義分頁(yè)效果

    這篇文章主要為大家詳細(xì)介紹了js css自定義分頁(yè)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • js前端存儲(chǔ)之sessionStorage使用方法舉例

    js前端存儲(chǔ)之sessionStorage使用方法舉例

    sessionStorage是指本地存儲(chǔ)一個(gè)會(huì)話中的數(shù)據(jù),這些數(shù)據(jù)只有在同一個(gè)會(huì)話中的頁(yè)面才能訪問(wèn)并且當(dāng)會(huì)話結(jié)束后數(shù)據(jù)也隨之銷毀,這篇文章主要給大家介紹了關(guān)于js前端存儲(chǔ)之sessionStorage使用方法的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • js改變透明度實(shí)現(xiàn)輪播圖的算法

    js改變透明度實(shí)現(xiàn)輪播圖的算法

    這篇文章主要為大家詳細(xì)介紹了js改變透明度實(shí)現(xiàn)輪播圖的算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 寫(xiě)了幾個(gè)類,希望對(duì)大家有用。

    寫(xiě)了幾個(gè)類,希望對(duì)大家有用。

    寫(xiě)了幾個(gè)類,希望對(duì)大家有用。...
    2006-12-12

最新評(píng)論