JavaScript利用多彩線條擺出心形效果的示例代碼
演示

源碼展示
創(chuàng)建畫布
<canvas width="300" height="300" style="width:100%;height:100vh;" id="c"></canvas>
基礎(chǔ)樣式設(shè)置
overflow 語法: overflow:{1,2}= visible | hidden | scroll | auto
默認(rèn)值:visible
取值:
visible:不剪切內(nèi)容。hidden:將超出對象尺寸的內(nèi)容進(jìn)行裁剪,將不出現(xiàn)滾動條。scroll:將超出對象尺寸的內(nèi)容進(jìn)行裁剪,并以滾動條的方式顯示超出的內(nèi)容。auto:在需要時剪切內(nèi)容并添加滾動條,此為body對象和textarea的默認(rèn)值。
padding:[ | ]{1,4}
默認(rèn)值:看每個獨(dú)立屬性
相關(guān)屬性:[ padding-top ] || [ padding-right ] || [ padding-bottom ] || [padding-left ]
取值: :用長度值來定義內(nèi)補(bǔ)白。不允許負(fù)值:用百分比來定義內(nèi)補(bǔ)白。不允許負(fù)值
說明: 檢索或設(shè)置對象四邊的內(nèi)部邊距。
如果提供全部四個參數(shù)值,將按上、右、下、左的順序作用于四邊。 如果只提供一個,將用于全部的四邊。
如果提供兩個,第一個用于上、下,第二個用于左、右。 如果提供三個,第一個用于上,第二個用于左、右,第三個用于下。
內(nèi)聯(lián)對象可以使用該屬性設(shè)置左、右兩邊的內(nèi)補(bǔ)?。蝗粢O(shè)置上、下兩邊的內(nèi)補(bǔ)丁,必須先使該對象表現(xiàn)為塊級或內(nèi)聯(lián)塊級。
對應(yīng)的腳本特性為padding。
html,body{
border: 0;
padding: 0;
margin: 0;
overflow: hidden;
background: #000;
}
.info{
z-index:999;
position : absolute;
left:0;
top:0;
padding:10px;
color:#fff;
background: rgba(0,0,0,0.5);
text-transform:capitalize;
}
用js來設(shè)計動畫效果
定義變量
var canvas = document.getElementById('c');
var ctx = canvas.getContext("2d");
var height = void 0,width = void 0,innerpoints = [],outerpoints = [],particles = [];
var noofpoints = 200,trashold = 10;
var x = void 0,y = void 0,p = void 0,n = void 0,point = void 0,dx = void 0,dy = void 0,color = void 0;
deltaangle = Math.PI * 2 / noofpoints,
r = Math.min(height, width) * 0.5;
var distance = function distance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
};
var mapVal = function mapVal(num, in_min, in_max, out_min, out_max) {
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};
設(shè)置畫布resize
var resize = function resize() {
height = ctx.canvas.clientHeight;
width = ctx.canvas.clientWidth;
if (ctx.canvas.clientWidth !== canvas.width ||
ctx.canvas.clientHeight !== canvas.height)
{
console.log("resized");
canvas.width = width;
canvas.height = height;
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(-Math.PI);
innerpoints = [];
r = 10;
for (var i = deltaangle; i <= Math.PI * 2; i += deltaangle) {
x = r * 16 * Math.pow(Math.sin(i), 3);
y = r * (13 * Math.cos(i) - 5 * Math.cos(2 * i) - 2 * Math.cos(3 * i) - Math.cos(4 * i));
innerpoints.push({
x: x,
y: y });
x = 10 * r * 16 * Math.pow(Math.sin(i), 3);
y = 10 * r * (13 * Math.cos(i) - 5 * Math.cos(2 * i) - 2 * Math.cos(3 * i) - Math.cos(4 * i));
outerpoints.push({
x: x,
y: y });
var step = random(0.001, 0.003, true);
particles.push({
step: step,
x: x,
y: y });
}
}
};
對線條設(shè)計
var draw = function draw() {
ctx.fillStyle = "rgba(0,0,0,0.03)";
ctx.fillRect(-width, -height, width * 2, height * 2);
ctx.beginPath();
for (var i = 0; i < innerpoints.length; i++) {
s = outerpoints[i];
d = innerpoints[i];
point = particles[i];
if (distance(point.x, point.y, d.x, d.y) > 10) {
dx = d.x - s.x;
dy = d.y - s.y;
point.x += dx * point.step;
point.y += dy * point.step;
color = distance(0, 0, point.x, point.y);
ctx.beginPath();
ctx.fillStyle = "hsl(" + color % 360 + ",100%,50%)";
ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
} else {
point.x = d.x;
point.y = d.y;
ctx.beginPath();
ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
particles[i].x = s.x;
particles[i].y = s.y;
particles[i].step = random(0.001, 0.003, true);
}
}
};
到此這篇關(guān)于JavaScript利用多彩線條擺出心形效果的示例代碼的文章就介紹到這了,更多相關(guān)JavaScript線條擺出心形內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS.getTextContent(element,preformatted)使用介紹
JS.getTextContent獲取標(biāo)簽的文字想必大家并不陌生吧,下面為大家介紹下具體的使用方法,感興趣的朋友可以參考下2013-09-09
uni-app 百度語音識別文字并展示功能實(shí)現(xiàn)
這篇文章主要介紹了uni-app 百度語音識別文字并展示功能實(shí)現(xiàn),本文主要寫的是 uniapp實(shí)現(xiàn)語音輸入并展示在頁面上,純前端,不涉及后端,需要的朋友可以參考下2023-12-12
JS控制靜態(tài)頁面之間傳遞參數(shù)獲取參數(shù)并應(yīng)用的簡單實(shí)例
下面小編就為大家?guī)硪黄狫S控制靜態(tài)頁面之間傳遞參數(shù)獲取參數(shù)并應(yīng)用的簡單實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
JavaScript undefined及null區(qū)別實(shí)例解析
這篇文章主要介紹了JavaScript undefined及null區(qū)別實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
在js(jquery)中獲得文本框焦點(diǎn)和失去焦點(diǎn)的方法
文章介紹兩個方法和種是利用javascript onFocus onBlur來判斷焦點(diǎn)和失去焦點(diǎn),加一種是利用jquery $("p").blur(); 或$("p").blur(fn)來實(shí)現(xiàn),有需要的朋友可以參考一下2012-12-12
javascript使用 concat 方法對數(shù)組進(jìn)行合并的方法
這篇文章主要介紹了javascript使用 concat 方法對數(shù)組進(jìn)行合并的方法,本文介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友一起看看吧2016-09-09
HTML中Select不用Disabled實(shí)現(xiàn)ReadOnly的效果
Disabled ReadOnly之家的聯(lián)系2008-04-04

