Skip to content

Commit

Permalink
bug fixes on Pt constructor and CanvasSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
williamngan committed May 19, 2017
1 parent 1680d5e commit bdff227
Show file tree
Hide file tree
Showing 9 changed files with 1,377 additions and 137 deletions.
703 changes: 634 additions & 69 deletions dist/pts.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<html>
<head></head>
<head>

<style type="text/css">
body {
background: #cde;
}
</style>

</head>

<body>
e
<canvas id="pt" width="300" height="300"></canvas>
<script type="text/javascript" src="./dist/pts.js"></script>
</body>

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"vectorious": "^4.8.1"
},
"devDependencies": {
"@types/vectorious": "^4.3.3",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.0",
"chai": "^3.5.0",
Expand Down
118 changes: 77 additions & 41 deletions src/Bound.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,99 @@
import {Pt, IPt} from "./Pt"


export class Bound extends Pt {
export class Bound implements IPt{

protected _center:Pt = new Pt();
protected _size:Pt = new Pt();
protected _topLeft:Pt = new Pt();
protected _bottomRight:Pt = new Pt();

constructor(x?:number|Array<number>|IPt, y=0, z=0, w=0) {
super(x,y,z,w);
if (!this.z) this.z = 0;
this._update();
constructor( p1?:IPt, p2?:IPt ) {
if (!p2) {
this._size = new Pt(p1);
} else if (p1) {
this._topLeft = new Pt(p1);
this._bottomRight = new Pt(p2);
this._updateSize();
}
}

bound( x?:number|Array<number>|IPt, y?:number, z?:number, w?:number ) {
let b = Pt.getArgs( [x,y,z,w] );
this.set(5, (b[0] || 0));
this.set(6, (b[1] || 0));
this.set(7, (b[2] || 0));
this._update();
protected _updateSize() {
this._size = this._bottomRight.$subtract( this._topLeft ).abs();
this._updateCenter();
}

static fromBoundingRect( rect:ClientRect ) {
let b = new Bound();
if (rect.top) b.y = rect.top;
if (rect.left) b.x = rect.left;
b.width = (rect.width) ? rect.width : ((rect.right) ? rect.right-rect.left : 0);
b.height = (rect.height) ? rect.height : ((rect.bottom) ? rect.bottom-rect.top : 0);
return b;
protected _updateCenter() {
this._center = this._size.$scale(0.5).add( this._topLeft );
}

protected _updatePosFromTop() {
this._bottomRight = this._topLeft.$add( this._size );
this._updateCenter();
}

protected _updatePosFromBottom() {
this._topLeft = this._bottomRight.$subtract( this._size );
this._updateCenter();
}

protected _updatePosFromCenter() {
let half = this._size.$scale(0.5);
this._topLeft = this._center.$subtract( half );
this._bottomRight = this._center.$add( half );
}


public get size():Pt { return new Pt(this._size); }
public set size(p: Pt) {
this._size = new Pt(p);
this._updatePosFromTop();
}

protected _update( which?:string ) {
if (!which || which == "x" || which == "w") this.set(8, this.x + this.width/2);
if (!which || which == "y" || which == "h") this.set(9, this.y + this.height/2);
if (!which || which == "z" || which == "d") this.set(10, this.z + this.depth/2);
public get center():Pt { return new Pt(this._center); }
public set center( p:Pt ) {
this._center = new Pt(p);
this._updatePosFromCenter();
}

setSize( w:number, h:number, d?:number ):this {
this.width = w;
this.height = h;
if (d != undefined) this.depth = d;
return this;
public get topLeft():Pt { return new Pt(this._topLeft); }
public set topLeft( p:Pt ) {
this._topLeft = new Pt(p);
this._updateSize();
}

set x( _x:number ) { this.set(0, _x); this._update('x'); }
set y( _y:number ) { this.set(0, _y); this._update('y'); }
set z( _z:number ) { this.set(0, _z); this._update('z'); }
public get bottomRight():Pt { return new Pt(this._bottomRight); }
public set bottomRight( p:Pt ) {
this._bottomRight = new Pt(p);
this._updateSize();
}

get width():number { return this.get(5); }
set width( _w:number ) { this.set(5, _w); this._update('w'); }
public get width():number { return this._size.x; }
public set width( w:number ) {
this._size.x = w;
this._updatePosFromTop();
}

get height():number { return this.get(6); }
set height( _h:number ) { this.set(6, _h); this._update('h'); }
public get height():number { return this._size.y; }
public set height( h:number ) {
this._size.y = h;
this._updatePosFromTop();
}

get size():Pt { return new Pt( this.width, this.height, this.depth ); }
public get depth():number { return this._size.z; }
public set depth( d:number ) {
this._size.z = d;
this._updatePosFromTop();
}

get depth():number { return this.get(7); }
set depth( _d:number ) { this.set(7, _d); this._update('d'); }
public get x():number { return this.topLeft.x; }
public get y():number { return this.topLeft.y; }
public get z():number { return this.topLeft.z; }

get center():Pt { return new Pt( this.get(8), this.get(9), this.get(10) ); }
get centerX(): number { return this.get(8); }
get centerY(): number { return this.get(9); }
get centerZ(): number { return this.get(10); }
static fromBoundingRect( rect:ClientRect ) {
let b = new Bound( new Pt( rect.left||0, rect.top||0 ), new Pt( rect.right||0, rect.bottom||0 ) );
if (rect.width && rect.height) b.size = new Pt(rect.width, rect.height);
return b;
}

}
39 changes: 20 additions & 19 deletions src/CanvasSpace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ interface PtsCanvasRenderingContext2D extends CanvasRenderingContext2D {
backingStorePixelRatio?:number;
}

export class CanvasSpace extends Space {

export default class CanvasSpace extends Space {
protected _canvas:HTMLCanvasElement;

protected _container:Element;
protected _pixelScale = 1;
protected _autoResize = true;
protected _autoResize = false;
protected _bgcolor = "#F3F7FA";
protected _ctx:PtsCanvasRenderingContext2D;

Expand All @@ -29,7 +29,7 @@ export class CanvasSpace extends Space {
* @param elem Specify an element by its "id" attribute as string, or by the element object itself. An element can be an existing `<canvas>`, or a `<div>` container in which a new `<canvas>` will be created. If left empty, a `<div id="pt_container"><canvas id="pt" /></div>` will be added to DOM. Use css to customize its appearance if needed.
* @param callback an optional callback `function(boundingBox, spaceElement)` to be called when canvas is appended and ready. A "ready" event will also be fired from the `<canvas>` element when it's appended, which can be traced with `spaceInstance.space.addEventListener("ready")`
*/
constructor( elem:string|Element, callback:Function) {
constructor( elem:string|Element, callback?:Function) {
super();

var _selector:Element = null;
Expand Down Expand Up @@ -67,10 +67,10 @@ export class CanvasSpace extends Space {
}

// if size is known then set it immediately
if (_existed) {
let b = this._container.getBoundingClientRect();
this.resize( Bound.fromBoundingRect(b) );
}
// if (_existed) {
// let b = this._container.getBoundingClientRect();
// this.resize( Bound.fromBoundingRect(b) );
// }

// no mutation observer, so we set a timeout for ready event
setTimeout( this._ready.bind( this, callback ), 50 );
Expand All @@ -89,8 +89,10 @@ export class CanvasSpace extends Space {
private _ready( callback:Function ) {
if (!this._container) throw `Cannot initiate #${this.id} element`;

let b = this._container.getBoundingClientRect();
this.resize( Bound.fromBoundingRect(b) );
if (this._autoResize) {
let b = this._container.getBoundingClientRect();
this.resize( Bound.fromBoundingRect(b) );
}

this.clear( this._bgcolor );
this._canvas.dispatchEvent( new Event("ready") );
Expand All @@ -109,7 +111,7 @@ export class CanvasSpace extends Space {
setup( opt:{bgcolor?:string, resize?:boolean, retina?:boolean} ):this {
if (opt.bgcolor) this._bgcolor = opt.bgcolor;

this._autoResize = (opt.resize !== false)
this._autoResize = (opt.resize === true);

if (opt.retina !== false) {
let r1 = window.devicePixelRatio || 1
Expand Down Expand Up @@ -151,12 +153,11 @@ export class CanvasSpace extends Space {

this.bound = b;

let sw = Math.floor(this.bound.width * this._pixelScale);
let sh = Math.floor(this.bound.height * this._pixelScale);
this._canvas.width = sw;
this._canvas.height = sh;
this._canvas.style.width = sw + "px";
this._canvas.style.height = sh + "px";
let s = this.bound.size.$scale( this._pixelScale );
this._canvas.width = s.x;
this._canvas.height = s.y;
this._canvas.style.width = s.x + "px";
this._canvas.style.height = s.y + "px";

if (this._pixelScale != 1) this._ctx.scale( this._pixelScale, this._pixelScale );

Expand All @@ -177,9 +178,9 @@ export class CanvasSpace extends Space {

if (this._bgcolor && this._bgcolor != "transparent") {
this._ctx.fillStyle = this._bgcolor;
this._ctx.fillRect( 0, 0, this.bound.width, this.bound.height );
this._ctx.fillRect( 0, 0, this._canvas.width, this._canvas.height );
} else {
this._ctx.clearRect( 0, 0, this.bound.width, this.bound.height );
this._ctx.clearRect( 0, 0, this._canvas.width, this._canvas.height );
}

this._ctx.fillStyle = lastColor;
Expand Down
28 changes: 22 additions & 6 deletions src/Pt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export class Pt extends Vector implements IPt, Iterable<number> {

// as an object of {x, y?, z?, w?}
} else if (typeof args[0] === 'object' && !Array.isArray( args[0] )) {
let a:Array<string> = ["x", "y", "z", "w"];
for (let p of a) {
if ( args[0][p] == undefined) break;
pos.push( args[0][p] );
let a = ["x", "y", "z", "w"];
let p = args[0];
for (let i=0; i<a.length; i++) {
if ( (p.length && i>=p.length) || !(a[i] in p) ) break; // check for length and key exist
pos.push( p[ a[i] ] );
}

// as an array of values
Expand All @@ -48,7 +49,7 @@ export class Pt extends Vector implements IPt, Iterable<number> {
* Update the values of this Pt
* @param args a list of numbers, an array of number, or an object with {x,y,z,w} properties
*/
to( ...args:any[]):Pt {
to( ...args:any[]):this {
let p = Pt.getArgs( args );
for (let i=0; i<p.length; i++) {
this.set( i, p[i] );
Expand All @@ -57,6 +58,18 @@ export class Pt extends Vector implements IPt, Iterable<number> {
return this;
}

$add( p:Vector ):Pt { return new Pt(this).add( p ); }
$subtract( p:Vector ):Pt { return new Pt(this).subtract( p ); }
$scale( n:number ):Pt { return new Pt(this).scale(n); }
$normalize():Pt { return new Pt(this).normalize(); }
$project( p:Vector ):Pt { return new Pt(this).project( new Pt(p) ); }

multiply( n:number ):this { return this.scale( n ); }
$multiply( n:number ):Pt { return this.$scale( n ); }
divide( n:number ):this { return this.scale(1/n); }
$divide( n:number ):Pt { return this.$scale(1/n); }



/**
* Iterator implementation to support for ... of loop
Expand All @@ -74,7 +87,10 @@ export class Pt extends Vector implements IPt, Iterable<number> {
}
}


abs():Pt {
this.each( (p) => Math.abs(p) );
return this;
}



Expand Down
7 changes: 7 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import {Vector, Matrix} from 'vectorious';
import {Pt} from './Pt';
import {Pts} from './Pts';
import CanvasSpace from "./CanvasSpace";

window["Pt"] = Pt;
window["Pts"] = Pts;

var canvas = new CanvasSpace("#pt");
console.log(canvas);
canvas.setup({bgcolor: "#f00", resize: false });

/*
let vec = new Vector( [1000, 2, 3] ).add( new Vector( [2, 3, 4] ) );
console.log(vec.toString());
Expand All @@ -19,6 +24,8 @@ console.log( Matrix.add(m1, m2).toString() );
let pts = new Pts();
console.log( pts );
*/

// console.log(pts.toString());
// pts.pt(1,2,3);
// pts.pt(2,3,4);
Expand Down
6 changes: 6 additions & 0 deletions src/test/Pt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe('Pt', function() {
assert.equal(11346, p1.get(0) + p1.get(3) + p2.get(2) + p3.get(1) + p4.get(0));
});

it('should init with Pt object', function() {
let p = new Pt( new Pt(1,2,3) );
assert.equal( 6, p.x + p.y + p.z );
});


it('should init with Array of numbers', function() {
let p1 = new Pt([10,100,1000,10000]);
let p2 = new Pt([11,111,1111]);
Expand Down
Loading

0 comments on commit bdff227

Please sign in to comment.