Skip to content

Commit

Permalink
Pre-support for blendMode as it is now supported in CSS too :)
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbany committed Feb 14, 2017
1 parent da225a3 commit fefb62b
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 26 deletions.
33 changes: 22 additions & 11 deletions src/scripting/Host/Unpacker/DisplayObject.ts
@@ -1,4 +1,8 @@
/// <reference path="DisplayObject.ts" />
/**
* Host end unpacker for generic DisplayObjects
* @author Jim Chen
*/
/// <reference path="Unpacker.ts" />
module Unpacker{
export class DisplayObject{
public DOM:HTMLDivElement;
Expand All @@ -12,9 +16,9 @@ module Unpacker{

constructor(stage:any, data:Object, context:any){
Unpacker.sensibleDefaults(data,{
"x":0,
"y":0,
"alpha":1
"x": 0,
"y": 0,
"alpha": 1
});
this.DOM = <HTMLDivElement> Unpacker._("div",{
"style": {
Expand Down Expand Up @@ -67,17 +71,24 @@ module Unpacker{
}

/** Transform **/
set transform(t:Object){
this._transform = t;
if(this._transform["mode"] === "2d"){

}else{

set transform(transformation:Object){
if (transformation.hasOwnProperty("mode")) {
this._transform = transformation;
if (transformation["mode"] === "2d") {
this.DOM.style.transform = "matrix2d(" +
transformation.matrix.join(",") + ")";
} else if (transformation["mode"] === "3d") {
this.DOM.style.transform = "matrix3d(" +
transformation.matrix.join(",") + ")";
} else {
throw new Error("Transform mode " +
transformation.mode + " not supported.");
}
}
}

get transform():Object{
return this._transform;
}
}
}
}
262 changes: 259 additions & 3 deletions src/scripting/Host/Unpacker/Shape.ts
@@ -1,9 +1,265 @@
/**
* Created by jim on 6/23/14.
* Host end unpacker for Shapes
* @author Jim Chen
*/

/// <reference path="Unpacker.ts" />
module Unpacker{
interface LineContext {
width: number;
color: string;
alpha: number;
caps: string;
joints: string;
miterLimit: string;
}
interface FillContext {
fill: string;
alpha: number;
fillRule: string;
}
interface GraphicsContext {
line:LineContext;
fill:FillContext;
}

export class Shape{
public DOM:Document;
private _x:number;
private _y:number;
private _alpha:number;
private _transform:Object = {
"mode": "3d",
"matrix": [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
};

private _defaultGroup:Element;
private _defaultEffect:Element;
private _defaultContainer:Element;
private _defaultGroupWithEffects:Element;

private _state:Object = {
"lastPath": null
};

private _graphics:GraphicsContext = {
"line": {
"width": 0,
"color": "#ffffff",
"alpha": 1,
"caps": null,
"joints": null,
"miterLimit": null
},
"fill": {
"fill": "none",
"alpha": 1,
"fillRule": "nonzero"
}
};

/**
* Creates elements with attributes or set attributes on existing ones
* @param element - string to create new element or existing element
* @param attr - map containing the attributes and values
* @return returns the element
*/
private static _svg(element:Element | string, attr:Object):Element {
var elem:Element;
if (typeof element === "string") {
// Create a new element
elem = document.createElementNS("http://www.w3.org/2000/svg", element);
} else {
elem = element;
}
if (attr !== null) {
for (var attrName in attr) {
elem.setAttribute(attrName, attr[attrName]);
}
}
return elem;
}

private static _position(x:number, y:number):string {
return "translate(" + x + "," + y + ")";
}

constructor(stage:any, data:Object, context:any) {
Unpacker.sensibleDefaults(data, {
"x": 0,
"y": 0,
"alpha": 1
});
this.DOM = Unpacker._("svg", {
"width": stage.offsetWidth,
"height": stage.offsetHeight,
"style": {
"position": "absolute",
"top": "0px",
"left": "0px",
"width": stage.offsetWidth + "px",
"height": stage.offsetHeight + "px",
"transform": "matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)"
}
});
// Create the default groups
this._defaultEffects = Shape._svg("defs", {});
this._defaultGroup = Shape._svg("g", {});
this._defaultContainer = Shape.svg("g", {
"transform": Shape._position(this._x, this._y),
"opacity": this._alpha
});
this._defaultGroupWithEffects = this._defaultContainer;

this._defaultContainer.appendChild(this._defaultGroup);
this.DOM.appendChild(this._defaultEffects);
this.DOM.appendChild(this._defaultGroupWithEffects);
}

set visible(visibility:boolean) {
this.DOM.style.visibility = visibilty ? "visible" : "hidden";
}

get visible():boolean {
return this.DOM.style.visibility === "hidden" ? false : true;
}

set x(x:number) {
this._x = x;
Shape._svg(this._defaultContainer, {
"transform": Shape._position(this._x, this._y)
});
}

set y(y:number) {
this._y = y;
Shape._svg(this._defaultContainer, {
"transform": Shape._position(this._x, this._y)
});
}

get x():number {
return this._x;
}

get y():number {
return this._y;
}

set alpha(alpha:number) {
this._alpha = alpha;
Shape._svg(this._defaultContainer, {
"opacity": this._alpha
});
}

get alpha():number {
return this._alpha;
}

set transform(transformation:Object) {
if (transformation.hasOwnProperty("mode")) {
this._transform = transformation;
if (transformation["mode"] === "2d") {
this.DOM.style.transform = "matrix(1,0,0,1,0,0)";
Shape._svg()
} else if (transformation["mode"] === "3d") {

}
}
}

get transform():Object {
return this._transform;
}

private applyFill(element:Element, context:FillContext) {
Shape._svg(element, {
"fill": context.fill,
"fill-opacity": context.alpha,
"fill-rule": context.fillRule
});
}

private applyStroke(element:Element, context:LineContext) {
Shape._svg(element, {
"stroke": context.color,
"stroke-width": context.width,
"stroke-opacity": context.alpha
});
if (context.caps !== null) {
p.setAttribute("stroke-linecap", context.caps);
}
if (context.joints !== null) {
p.setAttribute("stroke-linejoin", context.joints);
}
if (context.miterLimit !== null) {
p.setAttribute("stroke-miterlimit", context.miterLimit);
}
}

private _ensurePathStart() {
if (this._state.lastPath === null) {
this._state.lastPath = Shape._svg("path", {
"d": "M0 0"
});
this.applyFill(this._state.lastPath, this._graphics.fill);
this.applyStroke(this._state.lastPath, this._graphics.line);
defaultGroup.appendChild(this._state.lastPath);
}
}

public moveTo(params:Array) {
var p = Shape._svg("path", {
"d": "M" + params.join(" ")
});
this.applyFill(p, this._graphics.fill);
this.applyStroke(p, this._graphics.line);
this._state.lastPath = p;
this._defaultGroup.appendChild(this._state.lastPath);
}

public lineTo(params:Array) {
this._ensurePathStart();
Shape._svg(this._state.lastPath, {
"d": this._state.lastPath.getAttribute("d") + " L" + params.join(" ")
});
}

public curveTo(params:Array) {
this._ensurePathStart();
Shape._svg(this._state.lastPath, {
"d": this._state.lastPath.getAttribute("d") + " Q" + params.join(" ")
});
}

public lineStyle(params:Array) {
if (params.length < 3) {
throw new Error("Insufficient parameters, expected 3 or more.");
}
let context:LineContext = this._graphics.line;
context.width = params[0];
context.color = Unpacker.color(params[1]);
context.alpha = params[2];
if (params.length > 3) {
context.caps = params[3];
}
if (params.length > 4) {
context.joints = params[4];
}
if (params.length > 5) {
context.joints = params[5];
}
if (this._state.lastPath) {
this.applyStroke(this._state.lastPath, context);
}
}

public drawPath(params:Array) {

}

public beginFill(params:Array) {

}
}
}
}
11 changes: 7 additions & 4 deletions src/scripting/Host/Unpacker/TextField.ts
Expand Up @@ -25,12 +25,15 @@ module Unpacker{
this.DOM.style.fontFamily = tf["font"];
this.DOM.style.fontSize = tf["size"];
this.DOM.style.color = Unpacker.color(tf["color"]);
if(tf["bold"])
if (tf["bold"]) {
this.DOM.style.fontWeight = "bold";
if(tf["italic"])
}
if (tf["italic"]) {
this.DOM.style.fontStyle = "italic";
if(tf["underline"])
}
if (tf["underline"]) {
this.DOM.style.textDecoration = "underline";
}
this.DOM.style.margin = tf["margin"] + "px";
}

Expand All @@ -42,4 +45,4 @@ module Unpacker{
return this.DOM.textContent;
}
}
}
}
13 changes: 7 additions & 6 deletions src/scripting/Host/Unpacker/Unpacker.ts
@@ -1,5 +1,6 @@
module Unpacker{
export function _(type:string, props:Object = {}, children:Array<HTMLElement> = [], callback:Function = null):Element{
export function _(type:string, props:Object = {},
children:Array<HTMLElement> = [], callback:Function = null):Element{
var elem:Element = null;
if(type !== "svg"){
elem = document.createElement(type);
Expand Down Expand Up @@ -57,19 +58,19 @@ module Unpacker{
return objectA;
}

export function color(color:number):string{
if(typeof color === "string"){
export function color(color:number | string):string{
if (typeof color === "string") {
color = parseInt("" + color);
if(color === NaN){
if (color === NaN) {
color = 0;
}
}
var code:string = color.toString(16);
while(code.length < 6){
while (code.length < 6) {
code = "0" + code;
}
return "#" + code;
}
}

/// <reference path="TextField.ts" />
/// <reference path="TextField.ts" />

0 comments on commit fefb62b

Please sign in to comment.