bouncing-ball-d3
Version:
Bouncing Ball Graph Using d3 js
140 lines (133 loc) • 4.37 kB
JavaScript
import { select } from 'd3';
import { Injectable, Component, NgModule, defineInjectable } from '@angular/core';
/**
* @fileoverview added by tsickle
* Generated from: lib/bouncing-ball.service.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class BouncingBallService {
constructor() { }
}
BouncingBallService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
BouncingBallService.ctorParameters = () => [];
/** @nocollapse */ BouncingBallService.ngInjectableDef = defineInjectable({ factory: function BouncingBallService_Factory() { return new BouncingBallService(); }, token: BouncingBallService, providedIn: "root" });
/**
* @fileoverview added by tsickle
* Generated from: lib/bouncing-ball.component.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class BouncingBallComponent {
constructor() {
this.cx = 2;
this.cy = 2;
this.vx = 2;
this.vy = 5;
this.radius = 6;
this.gravity = 0.2;
this.damping = 0.85;
this.traction = 0.8;
this.paused = false;
this.width = 400;
this.height = 300;
}
/**
* @return {?}
*/
ngAfterContentInit() {
this.rect = select('svg').insert("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", this.width)
.attr("height", this.height)
.attr("fill", "black");
this.ball = select('svg').insert("circle")
.attr("cx", this.cx)
.attr("cy", this.cy)
.attr("r", this.radius)
.attr("fill", "blue");
}
/**
* @return {?}
*/
onStartClick() {
setInterval((/**
* @return {?}
*/
() => { this.drawBall(); }), 60);
}
/**
* @return {?}
*/
drawBall() {
this.ball.attr("cx", this.moveX()).attr("cy", this.moveY());
}
/**
* @return {?}
*/
moveX() {
if (this.cx + this.radius >= this.width) {
this.vx = -this.vx * this.damping;
this.cx = this.width - this.radius;
}
else if (this.cx - this.radius <= 0) {
this.vx = -this.vx * this.damping;
this.cx = this.radius;
}
this.cx += this.vx;
return this.cx;
}
/**
* @return {?}
*/
moveY() {
if (this.cy + this.radius >= this.height) {
this.vy = -this.vy * this.damping;
this.cy = this.height - this.radius;
this.vx *= this.traction;
}
else if (this.cy - this.radius <= 0) {
this.vy = -this.vy * this.damping;
this.cy = this.radius;
}
this.vy += this.gravity;
this.cy += this.vy;
return this.cy;
}
}
BouncingBallComponent.decorators = [
{ type: Component, args: [{
selector: 'd3-BouncingBall',
template: "<p>\r\n <svg width='100%' height='1200' class='mySvg'></svg>\r\n</p>\r\n"
}] }
];
/**
* @fileoverview added by tsickle
* Generated from: lib/bouncing-ball.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class BouncingBallModule {
}
BouncingBallModule.decorators = [
{ type: NgModule, args: [{
declarations: [BouncingBallComponent],
imports: [],
exports: [BouncingBallComponent]
},] }
];
/**
* @fileoverview added by tsickle
* Generated from: public_api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: bouncing-ball.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { BouncingBallService, BouncingBallComponent, BouncingBallModule };
//# sourceMappingURL=bouncing-ball.js.map