JavaScript canvas實(shí)現(xiàn)圍繞旋轉(zhuǎn)動(dòng)畫
使用canvas的convas來實(shí)現(xiàn)圍繞旋轉(zhuǎn)動(dòng)畫,外圈順時(shí)針,里層逆時(shí)針
代碼demo鏈接地址:代碼demo鏈接地址
html文件
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
</style>
<script src="js/konva.js"></script>
<script src="js/circle.js"></script>
</head>
<body>
<div id="cas"></div>
<script>
var width = window.innerWidth;
var height = window.innerHeight;
//創(chuàng)建舞臺(tái)
var stage = new Konva.Stage({
container: "cas",
width: width,
height: height
});
//創(chuàng)建層
var layer = new Konva.Layer();
//創(chuàng)建組1
var group = new Konva.Group({
x: stage.width() / 2,
y: stage.height() / 2,
});
//最外層圓
var circle1 = new Konva.Circle({
x: 0,
y: 0,
radius: 250,
stroke: "#ccc",
strokeWidth: 1,
dash: [6, 3]
});
group.add(circle1);
//第二個(gè)圓
var circle2 = new Konva.Circle({
x: 0,
y: 0,
radius: 150,
stroke: "#ccc",
strokeWidth: 1,
dash: [6, 3]
});
group.add(circle2);
//第三個(gè)圓
var circle3 = new Konva.Circle({
x: 0,
y: 0,
radius: 135,
stroke: "blue",
strokeWidth: 2,
dash: [10, 5]
});
group.add(circle3);
//第四個(gè)圓
var circle4 = new Konva.Circle({
x: 0,
y: 0,
radius: 105,
fill: "#ccc",
opacity: 0.4
});
group.add(circle4);
//第五個(gè)圓
var circle5 = new Konva.Circle({
x: 0,
y: 0,
radius: 80,
fill: "#74A2F0"
});
group.add(circle5);
//添加文字
var text = new Konva.Text({
x: -80,
y: -12,
text: "WEB全棧",
fill: "white",
fontSize: 24,
width: 160,
align: "center"
});
group.add(text);
layer.add(group);
//*****************************************************
//創(chuàng)建組2
var outGroup = new Konva.Group({
x: stage.width() / 2,
y: stage.height() / 2,
});
var arrColor = ["red", "green", "blue", "orange", "purple"];
var arrText = ["web", "node.js", "ajax", "html5", "css"];
for(var i = 0; i < 5; i++) {
var cir = new Circle({
x : 250 * Math.cos(72 * i * Math.PI / 180), //圓心x軸的坐標(biāo)
y : 250 * Math.sin(72 * i * Math.PI / 180), //圓心y軸的坐標(biāo)
outR : 60, //外圓的半徑
inR : 50, //內(nèi)圓的半徑
fill : arrColor[i], //填充顏色
text: arrText[i], //文字
outOpacity : 0.3, //外圓的透明度
inOpacity : 0.6 //內(nèi)圓的透明度
});
cir.drawCircle(outGroup);
}
layer.add(outGroup);
//***********************************************
//創(chuàng)建組3
var inGroup = new Konva.Group({
x: stage.width() / 2,
y: stage.height() / 2,
});
var arrColor = ["red", "green", "blue", "orange", "purple"];
var arrText = ["web", "node.js", "ajax", "html5", "css"];
for(var i = 0; i < 5; i++) {
var cir = new Circle({
x : 135 * Math.cos(90 * i * Math.PI / 180), //圓心x軸的坐標(biāo)
y : 135 * Math.sin(90 * i * Math.PI / 180), //圓心y軸的坐標(biāo)
outR : 45, //外圓的半徑
inR : 35, //內(nèi)圓的半徑
fill : arrColor[i], //填充顏色
text: arrText[i], //文字
outOpacity : 0.3, //外圓的透明度
inOpacity : 0.6 //內(nèi)圓的透明度
});
cir.drawCircle(inGroup);
}
layer.add(inGroup);
//************************************************
//運(yùn)動(dòng)動(dòng)畫
var step = 1; //轉(zhuǎn)動(dòng)的角度
var anim = new Konva.Animation(function() {
outGroup.rotate(step);
outGroup.getChildren().each(function (ele, index) {
ele.rotate(-step);
});
inGroup.rotate(-step);
inGroup.getChildren().each(function (ele, index) {
ele.rotate(step);
});
}, layer);
anim.start();
stage.add(layer);
stage.on("mouseover", function () {
step = 0.3;
});
stage.on("mouseout", function () {
step = 1;
});
</script>
</body>
</html>
js文件
function Circle(obj) {
this._init(obj);
}
Circle.prototype = {
_init: function (obj) {
this.x = obj.x, //圓心x軸的坐標(biāo)
this.y = obj.y, //圓心y軸的坐標(biāo)
this.outR = obj.outR, //外圓的半徑
this.inR = obj.inR, //內(nèi)圓的半徑
this.color = obj.fill, //填充顏色
this.text = obj.text, //內(nèi)圓的文字
this.outOpacity = obj.outOpacity, //外圓的透明度
this.inOpacity = obj.inOpacity //內(nèi)圓的透明度
},
drawCircle: function (group) {
//創(chuàng)建一個(gè)組
var groupCir = new Konva.Group({
x: this.x,
y: this.y
});
//外圓
var outCir = new Konva.Circle({
x: 0,
y: 0,
radius: this.outR,
fill: this.color,
opacity: this.outOpacity
});
groupCir.add(outCir);
//內(nèi)圓
var inCir = new Konva.Circle({
x: 0,
y: 0,
radius: this.inR,
fill: this.color,
opacity: this.inOpacity
});
groupCir.add(inCir);
//添加文字
var text = new Konva.Text({
x: -this.inR,
y: -10,
text: this.text,
fill: "white",
fontSize: 20,
width: 2 * this.inR,
align: "center"
});
groupCir.add(text);
group.add(groupCir);
}
}
效果圖片:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ES6基礎(chǔ)之展開語(yǔ)法(Spread syntax)
這篇文章主要介紹了ES6基礎(chǔ)之展開語(yǔ)法(Spread syntax),主要介紹了擴(kuò)展語(yǔ)法的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
教你用Uniapp實(shí)現(xiàn)微信小程序的GPS定位打卡
地圖組件用于展示地圖,而定位API只是獲取坐標(biāo),請(qǐng)勿混淆兩者,下面這篇文章主要給大家介紹了關(guān)于如何使用Uniapp實(shí)現(xiàn)微信小程序的GPS定位打卡的相關(guān)資料,需要的朋友可以參考下2022-11-11
解析瀑布流布局:JS+絕對(duì)定位的實(shí)現(xiàn)
本篇文章是對(duì)瀑布流局部的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
uniapp使用u-upload組件來實(shí)現(xiàn)圖片上傳功能
最近在用uniapp開發(fā)微信小程序,下面這篇文章主要給大家介紹了關(guān)于uniapp使用u-upload組件來實(shí)現(xiàn)圖片上傳功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
IE6-8中Date不支持toISOString的修復(fù)方法
這篇文章主要介紹了IE6-8中Date不支持toISOString的修復(fù)方法,需要的朋友可以參考下2014-05-05
基于JavaScript實(shí)現(xiàn)選項(xiàng)卡效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)選項(xiàng)卡效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
JavaScript中的數(shù)學(xué)運(yùn)算介紹
這篇文章主要介紹了JavaScript中的數(shù)學(xué)運(yùn)算介紹,本文先是講解了數(shù)學(xué)運(yùn)算的一些知識(shí),然后給出了操作實(shí)例,需要的朋友可以參考下2014-12-12
three.js中3D視野的縮放實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了three.js中3D視野的縮放實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

