天介紹了前端系列:如何用CSS代碼來寫26個字母,前端福利,沒有看過的可以看我之前的文章,或者打開下面的網(wǎng)址來瀏覽:http://www.toutiao.com/i6366084511695897089/
今天介紹的是,用css代碼來寫幾何圖形或者不規(guī)則的圖形,當(dāng)然看這些都需要一點(diǎn)點(diǎn)的div+css的基礎(chǔ),還有了解新的html5+css3的知識點(diǎn),這樣你才能更好的體會感觸。
正方形代碼
長方形代碼
圓形代碼
橢圓的代碼
正三角形的代碼
倒三角行的代碼
左三角形代碼
右三角形代碼
梯形代碼
愛心代碼
太極代碼
還有一些其他的代碼,就不一一的發(fā)圖了,有興趣的可以打開下面的網(wǎng)站來看這些代碼
https://css-tricks.com/examples/ShapesOfCSS/
馨客棧導(dǎo)航:http://www.mackxin.com/nav.html
馨客棧交流:http://www.mackxin.com/xininn.html
關(guān)注分享,體驗(yàn)樂趣
分享是一種態(tài)度
學(xué)無止境,學(xué)海無涯,好好學(xué)習(xí),天天向上
近公司項(xiàng)目也挺忙的,所以很少時間來寫文章。最近公司有個需求要實(shí)現(xiàn)帶曲線的上升效果,我這邊實(shí)現(xiàn)了安卓版本,今天特意用js也寫了一份,就當(dāng)時學(xué)習(xí)了。JS最終實(shí)現(xiàn)如下圖,這個圖片是不動的,打開是有動畫的。
js曲線學(xué)習(xí)
大致就是這么一個效果,但是里面涉及的邏輯還是挺多的。因?yàn)橛泻脦讖圲I效果圖,目前我就貼出來一張,如果能實(shí)現(xiàn)這一張的效果基本其他的也容易搞定了,但是這里我就不分享Android的代碼了,畢竟是公司項(xiàng)目,所以很多信息必透露,這里分享JS實(shí)現(xiàn)的代碼。
原圖
<html>
<head>
</head>
<body>
<canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
當(dāng)前瀏覽器不支持Canvas,請更換瀏覽器后再試
</canvas>
<script>
function l(){
this.canvas; // canvas節(jié)點(diǎn)
this.ctx; // canvas繪圖上下文
this.x; // 橫向坐標(biāo)
this.y; // 縱向坐標(biāo)
this.rx; //橢圓長軸
this.ry; //橢圓短軸
this.degree1;//初始角度
this.degree2;//結(jié)束角度
this.degreecur; //當(dāng)前角度
};
l.prototype.init = function(){
var canvas = document.getElementById("canvas");
var context=canvas.getContext('2d');
canvas.width = 800;
canvas.height = 800;
this.canvas = canvas;
this.ctx = context;
this.degree1 = 20;
this.degree2 = 120;
this.degreecur = this.degree2;
this.x = 200;
this.y = 200;
this.rx = 150;
this.ry = 50;
this.increase = 1;
};
l.prototype.update = function(){
this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
this.ctx.lineWidth = 2;
// 繪制弧線
this.drawLine('#000', this.degree1, this.degree2);
this.drawLine('#f00', this.degreecur, this.degree2);
this.drawCr();
this.degreecur -= this.increase;
if(this.degreecur < this.degree1){
this.degreecur = this.degree2;
}
};
l.prototype.drawCr = function(){
var o = this.degreecur * Math.PI/180;
var a = this.rx;
var b = this.ry;
var x = 0;
var y = 0;
x = a * Math.cos(o);
y = b * Math.sin(o);
x += this.x;
y += this.y;
this.ctx.beginPath();
this.ctx.strokeStyle = '#0f0';
this.ctx.fillStyle = '#0f0';
this.ctx.arc(x,y,5,0,2*Math.PI);
this.ctx.fill();
this.ctx.closePath();
};
l.prototype.drawLine = function(color, degreeStart, degreeEnd){
this.ctx.beginPath();
this.ctx.strokeStyle = color;
//起點(diǎn)x.起點(diǎn)y,半徑x,半徑y(tǒng),旋轉(zhuǎn)的角度,起始角,結(jié)果角,順時針還是逆時針
this.ctx.ellipse(this.x,this.y,this.rx,this.ry,0,degreeStart*Math.PI/180,degreeEnd*Math.PI/180,false);
this.ctx.stroke();
this.ctx.closePath();
};
var l1 = new l();
// dot移動效果
function animateUpdate() {
l1.update(); // 更新dot的當(dāng)前位置
setTimeout(animateUpdate, 25);
}
window.onload = function(){
l1.init();
animateUpdate()
}
</script>
</body>
</html>
如果需要驗(yàn)證的可以直接復(fù)制上面的代碼運(yùn)行就能看到效果。這里不做演示,本次分享就到此為止,謝謝大家了。如果喜歡就關(guān)注,點(diǎn)贊吧,謝謝了。
家好,我是一碗周,一個不想被喝(內(nèi)卷)的前端。如果寫的文章有幸可以得到你的青睞,萬分有幸~
過年了~ 過年了~ 過年了~
辭舊迎新過年啦 張燈結(jié)彩春節(jié)啦~
金雞起舞送福啦 新的一年福來啦~
文章開頭幾句歌詞,頓時顯得喜慶了不,我們的燈籠是下面這個樣子的。
畫燈籠我們肯定不能畫一個靜態(tài)的燈籠,我們先來復(fù)習(xí)一下CSS中的 animation 屬性,該是是一個簡寫屬性,由 animation-name , animation-duration , animation-timing-function , animation-delay , animation-iteration-count , animation-direction , animation-fill-mode 和 animation-play-state 屬性組成。這里我們就不展開講解了,具體可以跳轉(zhuǎn)到MDN學(xué)習(xí)。
我們先來看一下下面這個示例:
animation: swing 3s infinite ease-in-out;
在上面的例子中使用了一個名為 swing 的動畫序列,動畫序列通過 @keyframes 創(chuàng)建,執(zhí)行時間 3s ,動畫循環(huán)執(zhí)行,最后 ease-in-out 表示動畫執(zhí)行的節(jié)奏。
接下來我們就分步驟實(shí)現(xiàn)。
首先我們定義HTML結(jié)構(gòu),代碼如下:
<!-- 燈籠容器 -->
<div class="lantern-con">
<!-- 提著燈籠的線 -->
<div class="lantern-line"></div>
<!-- 燈籠主要區(qū)域 -->
<div class="lantern-light">
</div>
</div>
然后我們畫一個橢圓,然后通過 ::before 和 ::after ,繪制上下的兩個燈籠蓋,CSS如下:
/* 燈籠容器 */
.lantern-con {
position: fixed;
left: 160px;
}
/* 燈籠中間紅色區(qū)域 */
.lantern-light {
position: relative;
width: 120px;
height: 90px;
background-color: red;
margin: 30px;
border-radius: 50%;
box-shadow: -5px 5px 50px 4px #fa6c00;
/* 設(shè)置旋轉(zhuǎn)點(diǎn) */
transform-origin: top center;
animation: swing 3s infinite ease-in-out;
}
/* 燈籠頂部和底部的樣式 */
.lantern-light::before,
.lantern-light::after {
content: '';
position: absolute;
border: 1px solid #dc8f03;
width: 60px;
height: 12px;
/* 背景漸變 */
background: linear-gradient(
to right,
#dc8f03,
#ffa500,
#dc8f03,
#ffa500,
#dc8f03
);
left: 30px;
}
/* 頂部位置 */
.lantern-light::before {
top: -7px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
/* 底部位置 */
.lantern-light::after {
bottom: -7px;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
/* 提著燈籠的線的樣式 */
.lantern-line {
width: 2px;
height: 50px;
background-color: #dc8f03;
position: absolute;
left: 88px;
}
/* 燈籠的動畫效果 */
@keyframes swing {
0% {
transform: rotate(-6deg);
}
50% {
transform: rotate(6deg);
}
100% {
transform: rotate(-6deg);
}
}
現(xiàn)在就實(shí)現(xiàn)了一個比較基礎(chǔ)燈籠外形,效果如下:
燈籠的內(nèi)部線條是通過兩個矩形實(shí)現(xiàn)了,通過 border-radius 屬性將其設(shè)置為橢圓,然后繪制邊,呈現(xiàn)燈籠線條。
<div class="lantern-light">
<!-- 燈籠中間的線條 -->
<div class="lantern-circle">
<div class="lantern-rect">
<!-- 燈籠中間的文字內(nèi)容 -->
<div class="lantern-text">燈籠</div>
</div>
</div>
</div>
對應(yīng)的CSS如下:
/* 燈籠中間的線條 */
.lantern-circle,
.lantern-rect {
height: 90px;
border-radius: 50%;
border: 2px solid #dc8f03;
background-color: rgba(216, 0, 15, 0.1);
}
/* 外層 */
.lantern-circle {
width: 100px;
margin: 12px 8px 8px 10px;
}
/* 內(nèi)層 */
.lantern-rect {
margin: -2px 8px 8px 26px;
width: 45px;
}
/* 文字樣式 */
.lantern-text {
font-size: 28px;
font-weight: bold;
text-align: center;
color: #dc8f03;
margin-top: 4px;
}
現(xiàn)在我們來繪制一下燈籠穗,這個東西比較簡單,最主要的是添加一個動畫效果,其HTML結(jié)構(gòu)如下:
<!-- 燈籠主要區(qū)域 -->
<div class="lantern-light">
<!-- more code -->
<!-- 燈籠穗 -->
<div class="lantern-tassel-top">
<div class="lantern-tassel-middle"></div>
<div class="lantern-tassel-bottom"></div>
</div>
</div>
主要區(qū)域 -->
CSS如下:
/* 燈穗 */
.lantern-tassel-top {
width: 5px;
height: 20px;
background-color: #ffa500;
border-radius: 0 0 5px 5px;
position: relative;
margin: -5px 0 0 59px;
/* 讓燈穗也有一個動畫效果 */
animation: swing 3s infinite ease-in-out;
}
.lantern-tassel-middle,
.lantern-tassel-bottom {
position: absolute;
width: 10px;
left: -2px;
}
.lantern-tassel-middle {
border-radius: 50%;
top: 14px;
height: 10px;
background-color: #dc8f03;
z-index: 2;
}
.lantern-tassel-bottom {
background-color: #ffa500;
border-bottom-left-radius: 5px;
height: 35px;
top: 18px;
z-index: 1;
}
到這我們就把這個燈籠畫完了。
本篇文章到這就結(jié)束了,都看完了就點(diǎn)個贊支持一下,謝謝了~
原作者姓名: 一碗周
原文鏈接: https:// juejin.cn/post/70513709 71932033038
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。