aboutsummaryrefslogtreecommitdiff
path: root/assets/canvas-runtime.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/canvas-runtime.js')
-rw-r--r--assets/canvas-runtime.js64
1 files changed, 49 insertions, 15 deletions
diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js
index ec5097bc..c0c4ccfc 100644
--- a/assets/canvas-runtime.js
+++ b/assets/canvas-runtime.js
@@ -8,9 +8,37 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
8var NinjaCvsRt = NinjaCvsRt || {}; 8var NinjaCvsRt = NinjaCvsRt || {};
9 9
10/////////////////////////////////////////////////////////////////////// 10///////////////////////////////////////////////////////////////////////
11//Loading webGL/canvas data on window load
12window.addEventListener('load', loadCanvasData, false);
13//Load data function (on document loaded)
14function loadCanvasData (e) {
15 //Cleaning up events
16 window.removeEventListener('load', loadCanvasData, false);
17 //Getting tag with data, MUST contain attribute
18 var xhr, tag = document.querySelectorAll(['script[data-ninja-canvas-lib]'])[0];
19 //Checking for data to be external file
20 if (tag.getAttribute('data-ninja-canvas-json') !== null) {
21 //Loading JSON data
22 xhr = new XMLHttpRequest();
23 xhr.open("GET", tag.getAttribute('data-ninja-canvas-json'), false);
24 xhr.send();
25 //Checking for data
26 if (xhr.readyState === 4) {
27 //Calling method to initialize all webGL/canvas(es)
28 NinjaCvsRt.initWebGl(document.body, tag.getAttribute('data-ninja-canvas-libpath'), xhr.response);
29 } else {
30 //TODO: Add error for users
31 }
32 } else {//Data in document itself
33 //Calling method to initialize all webGL/canvas(es)
34 NinjaCvsRt.initWebGl(document.body, tag.getAttribute('data-ninja-canvas-libpath'), document.querySelectorAll(['script[data-ninja-canvas]'])[0].innerHTML);
35 }
36}
37
38///////////////////////////////////////////////////////////////////////
11//Loading webGL/canvas data 39//Loading webGL/canvas data
12NinjaCvsRt.initWebGl = function (rootElement, directory) { 40NinjaCvsRt.initWebGl = function (rootElement, directory, data) {
13 var cvsDataMngr, ninjaWebGlData = JSON.parse((document.querySelectorAll(['script[data-ninja-webgl]'])[0].innerHTML.replace('(', '')).replace(')', '')); 41 var cvsDataMngr, ninjaWebGlData = JSON.parse((data.replace('(', '')).replace(')', ''));
14 if (ninjaWebGlData && ninjaWebGlData.data) { 42 if (ninjaWebGlData && ninjaWebGlData.data) {
15 for (var n=0; ninjaWebGlData.data[n]; n++) { 43 for (var n=0; ninjaWebGlData.data[n]; n++) {
16 ninjaWebGlData.data[n] = unescape(ninjaWebGlData.data[n]); 44 ninjaWebGlData.data[n] = unescape(ninjaWebGlData.data[n]);
@@ -882,11 +910,12 @@ NinjaCvsRt.RuntimeRectangle = Object.create(NinjaCvsRt.RuntimeGeomObj, {
882 inset = Math.ceil( lw ) - 0.5; 910 inset = Math.ceil( lw ) - 0.5;
883 911
884 if(this._fillColor.gradientMode) { 912 if(this._fillColor.gradientMode) {
885 if(this._fillColor.gradientMode === "radial") { 913 if(this._fillColor.gradientMode === "radial") {
886 gradient = ctx.createRadialGradient(w/2, h/2, 0, w/2, h/2, Math.max(w, h)/2); 914 var ww = w - 2*lw, hh = h - 2*lw;
887 } else { 915 gradient = ctx.createRadialGradient(w/2, h/2, 0, w/2, h/2, Math.max(ww, hh)/2);
888 gradient = ctx.createLinearGradient(inset/2, h/2, w-inset, h/2); 916 } else {
889 } 917 gradient = ctx.createLinearGradient(inset, h/2, w-inset, h/2);
918 }
890 colors = this._fillColor.color; 919 colors = this._fillColor.color;
891 920
892 len = colors.length; 921 len = colors.length;
@@ -916,11 +945,10 @@ NinjaCvsRt.RuntimeRectangle = Object.create(NinjaCvsRt.RuntimeGeomObj, {
916 inset = Math.ceil( 0.5*lw ) - 0.5; 945 inset = Math.ceil( 0.5*lw ) - 0.5;
917 946
918 if(this._strokeColor.gradientMode) { 947 if(this._strokeColor.gradientMode) {
919 if(this._strokeColor.gradientMode === "radial") { 948 if(this._strokeColor.gradientMode === "radial")
920 gradient = ctx.createRadialGradient(w/2, h/2, Math.min(h, w)/2-inset, w/2, h/2, Math.max(h, w)/2); 949 gradient = ctx.createRadialGradient(w/2, h/2, 0, w/2, h/2, Math.max(h, w)/2);
921 } else { 950 else
922 gradient = ctx.createLinearGradient(0, h/2, w, h/2); 951 gradient = ctx.createLinearGradient(0, h/2, w, h/2);
923 }
924 colors = this._strokeColor.color; 952 colors = this._strokeColor.color;
925 953
926 len = colors.length; 954 len = colors.length;
@@ -1120,9 +1148,9 @@ NinjaCvsRt.RuntimeOval = Object.create(NinjaCvsRt.RuntimeGeomObj, {
1120 if(this._fillColor.gradientMode) { 1148 if(this._fillColor.gradientMode) {
1121 if(this._fillColor.gradientMode === "radial") { 1149 if(this._fillColor.gradientMode === "radial") {
1122 gradient = ctx.createRadialGradient(xCtr, yCtr, 0, 1150 gradient = ctx.createRadialGradient(xCtr, yCtr, 0,
1123 xCtr, yCtr, Math.max(this._width, this._height)/2); 1151 xCtr, yCtr, Math.max(this._width, this._height)/2 - lineWidth);
1124 } else { 1152 } else {
1125 gradient = ctx.createLinearGradient(lineWidth/2, this._height/2, this._width-lineWidth, this._height/2); 1153 gradient = ctx.createLinearGradient(lineWidth, this._height/2, this._width-lineWidth, this._height/2);
1126 } 1154 }
1127 colors = this._fillColor.color; 1155 colors = this._fillColor.color;
1128 1156
@@ -1198,7 +1226,7 @@ NinjaCvsRt.RuntimeOval = Object.create(NinjaCvsRt.RuntimeGeomObj, {
1198 if (this._strokeColor) { 1226 if (this._strokeColor) {
1199 if(this._strokeColor.gradientMode) { 1227 if(this._strokeColor.gradientMode) {
1200 if(this._strokeColor.gradientMode === "radial") { 1228 if(this._strokeColor.gradientMode === "radial") {
1201 gradient = ctx.createRadialGradient(xCtr, yCtr, Math.min(xScale, yScale), 1229 gradient = ctx.createRadialGradient(xCtr, yCtr, 0,
1202 xCtr, yCtr, 0.5*Math.max(this._height, this._width)); 1230 xCtr, yCtr, 0.5*Math.max(this._height, this._width));
1203 } else { 1231 } else {
1204 gradient = ctx.createLinearGradient(0, this._height/2, this._width, this._height/2); 1232 gradient = ctx.createLinearGradient(0, this._height/2, this._width, this._height/2);
@@ -1460,6 +1488,8 @@ NinjaCvsRt.RuntimeRadialGradientMaterial = Object.create(NinjaCvsRt.RuntimeMater
1460 _colorStop3: { value: 0.6, writable: true }, 1488 _colorStop3: { value: 0.6, writable: true },
1461 _colorStop4: { value: 1.0, writable: true }, 1489 _colorStop4: { value: 1.0, writable: true },
1462 1490
1491 _textureTransform: { value: [1,0,0, 0,1,0, 0,0,1], writable: true },
1492
1463 init: { 1493 init: {
1464 value: function(world) { 1494 value: function(world) {
1465 var material = this._materialNode; 1495 var material = this._materialNode;
@@ -1481,6 +1511,8 @@ NinjaCvsRt.RuntimeRadialGradientMaterial = Object.create(NinjaCvsRt.RuntimeMater
1481 this._shader["default"].u_colorStop3.set( [this._colorStop3] ); 1511 this._shader["default"].u_colorStop3.set( [this._colorStop3] );
1482 this._shader["default"].u_colorStop4.set( [this._colorStop4] ); 1512 this._shader["default"].u_colorStop4.set( [this._colorStop4] );
1483 1513
1514 this._shader["default"].u_texTransform.set( this._textureTransform );
1515
1484 if (this._angle !== undefined) 1516 if (this._angle !== undefined)
1485 this._shader["default"].u_cos_sin_angle.set([Math.cos(this._angle), Math.sin(this._angle)]); 1517 this._shader["default"].u_cos_sin_angle.set([Math.cos(this._angle), Math.sin(this._angle)]);
1486 } 1518 }
@@ -1500,6 +1532,8 @@ NinjaCvsRt.RuntimeRadialGradientMaterial = Object.create(NinjaCvsRt.RuntimeMater
1500 this._colorStop3 = jObj.colorStop3; 1532 this._colorStop3 = jObj.colorStop3;
1501 this._colorStop4 = jObj.colorStop4; 1533 this._colorStop4 = jObj.colorStop4;
1502 1534
1535 this._textureTransform = jObj.textureTransform;
1536
1503 if (this._angle !== undefined) 1537 if (this._angle !== undefined)
1504 this._angle = jObj.angle; 1538 this._angle = jObj.angle;
1505 } 1539 }