1.html代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>02面向对象版本的进度条</title>
<style>
body {
padding: 0;
margin: 0;
background-color: #f0f0f0;
overflow: hidden;
}
</style>
<script src="konva.js"></script>
<script src="progressBar.js"></script>
</head>
<body>
<div id="container">
</div>
<script>
//创建舞台
var stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,//全屏
height: window.innerHeight
});
//创建层
var layer = new Konva.Layer();
stage.add(layer);
//中心点坐标
var cenX = stage.width() / 2;
var cenY = stage.height() / 2;
var option = {//把参数设置成对象形式;
x: 1/8 * stage.width(), //进度条x坐标
y: 1/2 * stage.height(), //进度条y坐标
w: 3/4 * stage.width(), //进度条的宽度
h: 1/12 * stage.height(), //进度条的高度
fillStyle: 'red', //进度条内部矩形的填充的颜色
strokeStyle: 'blue' //进度条的外边框的颜色
};
// 创建进度条对象实例;在new的时候调用初始化值;
var p = new ProgressBar( option );
//把进度条放到 层中
p.addToGroupOrLayer( layer );
//渲染层
layer.draw();
p.changeValue( .5 );
</script>
</body>
</html>

2.js代码:

 function ProgressBar( option ) {
//new 构造函数执行的时候,调用 内部的初始化方法。
this._init( option );
} ProgressBar.prototype = {
//类初始化的时候:创建内部矩形, 外部矩形,然后把两个矩放到goroup里面去。
_init: function( option ) {
this.x = option.x || 0; //进度条的x坐标
this.y = option.y || 0; // 进度条的y坐标
this.w = option.w || 0; //进度条的宽度
this.h = option.h || 0; //进度条的高度 this.fillStyle = option.fillStyle || 'red';
this.strokeStyle = option.strokeStyle || 'red'; //定义的内部的进度条的矩形
var innerRect = new Konva.Rect({
x: this.x, // stage.width(),获得舞台的宽度, x:设置当前矩形x坐标
y: this.y,
width: 0, //设置矩形的宽度
height: this.h, //设置矩形的高度
fill: this.fillStyle, //设置矩形的填充的颜色
cornerRadius: 1/2 * this.h, //设置进度条的圆角。
id: 'innerRect', //设置当前矩形的ID,以便于后面进行使用ID选择器
name: 'ss' //设置name,方便后面用类选择器。
}); this.innerRect = innerRect; //添加一个外边框的 矩形
var outerRect = new Konva.Rect({
x: this.x, // stage.width(),获得舞台的宽度, x:设置当前矩形x坐标
y: this.y,
width: this.w, //设置矩形的宽度
height: this.h, //设置矩形的高度形的高度
stroke: this.strokeStyle, // 设置了stroke那么,矩形就进行描边
strokeWidth: 4, // 设置边框的宽度,
cornerRadius: 1/2* this.h,
}); //html :
// 创建一个组, 相当于html中的父盒子。把其他两个盒子包在里面;当给其设置坐标时,相当于进行绝对定位,这时候的子盒子是跟着父盒子进行定位的;
this.group = new Konva.Group({
x: 0,
y: 0
});
this.group.add( innerRect );//把内部的矩形放到组中
this.group.add( outerRect );
},
//此方法是将 用户传进来的需要改变的进度 运行动画
changeValue: function( val ) {
//传进来的进度
if( val > 1 ) { // 1 - 100 vs 0 -1 =>0.5
val = val /100;
}
//做动画 val = .3 .7
var width = this.w * val; //最终进度条内部矩形的 宽度。 // 通过id选择器去查找内部的子元素。
var innerRect = this.group.findOne('#innerRect');
// var innerRect = this.group.findOne('Rect'); var innerRect = this.innerRect; // to动画系统: 让我们的物件 变换到某个状态
// 让我们的 物件从 当前状态 到 下面设置的状态,
innerRect.to({
width: width,
duration: .3,
easing: Konva.Easings.EasIn
}); },
// arg: 传进来的层或者 是组,
//此方法是:把当前创建的进度条 添加到 层中。
addToGroupOrLayer: function( layer ) {
layer.add( this.group );
}
}
05-11 13:45