mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-04-23 15:24:43 +00:00
Add demo of an animating cross (demonstrating how to create a custom class).
This commit is contained in:
parent
f37b93e6c1
commit
dcaee8bf0c
2 changed files with 93 additions and 1 deletions
91
demos/cross/index.html
Normal file
91
demos/cross/index.html
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Animating crosses (custom class)</title>
|
||||
<script src="../../dist/all.js"></script>
|
||||
<style>
|
||||
.canvas-container { border: 1px solid #ccc; display: inline-block; vertical-align: top; }
|
||||
h2 span { font-weight: normal; }
|
||||
pre { background: #eef; display: inline-block; padding-right: 15px; margin-left: 15px }
|
||||
</style>
|
||||
<link rel="stylesheet" href="../../lib/master.css" type="text/css">
|
||||
</head>
|
||||
<body>
|
||||
<ul id="header">
|
||||
<li><a href="../index.html">Demos</a></li>
|
||||
<li><a href="../../docs/index.html">Docs</a></li>
|
||||
<li><a href="../../test/unit/suite_runner.html">Tests</a></li>
|
||||
</ul>
|
||||
<div id="bd-wrapper">
|
||||
|
||||
<h2><span>Fabric.js demos</span> · Animating crosses (custom class)</h2>
|
||||
|
||||
<canvas id="c" width="600" height="500"></canvas>
|
||||
|
||||
<script id="main">
|
||||
var canvas = new fabric.Element('c');
|
||||
|
||||
var Cross = fabric.util.createClass(fabric.Object, {
|
||||
|
||||
initialize: function(options) {
|
||||
this.callSuper('initialize', options);
|
||||
this.animDirection = 'up';
|
||||
|
||||
this.width = 100;
|
||||
this.height = 100;
|
||||
|
||||
this.w1 = this.h2 = 100;
|
||||
this.h1 = this.w2 = 30;
|
||||
},
|
||||
|
||||
animateWidthHeight: function() {
|
||||
var interval = 2;
|
||||
|
||||
if (this.h2 >= 30 && this.h2 <= 100) {
|
||||
var actualInterval = (this.animDirection === 'up' ? interval : -interval);
|
||||
this.h2 += actualInterval;
|
||||
this.w1 += actualInterval;
|
||||
}
|
||||
|
||||
if (this.h2 >= 100) {
|
||||
this.animDirection = 'down';
|
||||
this.h2 -= interval;
|
||||
this.w1 -= interval;
|
||||
}
|
||||
if (this.h2 <= 30) {
|
||||
this.animDirection = 'up';
|
||||
this.h2 += interval;
|
||||
this.w1 += interval;
|
||||
}
|
||||
},
|
||||
|
||||
_render: function(ctx) {
|
||||
ctx.fillRect(-this.w1 / 2, -this.h1 / 2, this.w1, this.h1);
|
||||
ctx.fillRect(-this.w2 / 2, -this.h2 / 2, this.w2, this.h2);
|
||||
}
|
||||
});
|
||||
|
||||
canvas.add(new Cross({ top: 100, left: 100 }));
|
||||
canvas.add(new Cross({ top: 140, left: 230 }));
|
||||
canvas.add(new Cross({ top: 300, left: 210 }));
|
||||
canvas.add(new Cross({ top: 40, left: 400 }));
|
||||
canvas.add(new Cross({ top: 450, left: 400 }));
|
||||
|
||||
setTimeout(function animate() {
|
||||
canvas.forEachObject(function(obj){ obj.animateWidthHeight() });
|
||||
canvas.renderAll();
|
||||
setTimeout(animate, 10);
|
||||
}, 10);
|
||||
</script>
|
||||
|
||||
<script>
|
||||
(function(){
|
||||
var preEl = document.createElement('pre');
|
||||
preEl.innerHTML = document.getElementById('main').innerHTML;
|
||||
document.getElementById('bd-wrapper').appendChild(preEl);
|
||||
})();
|
||||
</script>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -13,7 +13,8 @@
|
|||
</ul>
|
||||
<div id="bd-wrapper">
|
||||
<ul>
|
||||
<li><a href="ladybug/index.html">Ladybugs</a></li>
|
||||
<li style="margin-bottom:5px"><a href="ladybug/index.html">Ladybugs</a></li>
|
||||
<li style="margin-bottom:5px"><a href="cross/index.html">Crosses</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
|||
Loading…
Reference in a new issue