1 Reply Latest reply on Oct 31, 2017 1:43 AM by Colin Holgate

    Linking to an Inlined Image - Html Canvas

    igloocreativehouse

      Hi!

       

      We're making an HTML ad,

      but some of the interfaces we're trying to upload it to

      require everything to be included in one single HTML file.

       

      So we are trying to embed a spriteSheet (image) to the exported html file.

      we succeded in interpreting the image into code,

      but we are struggling with where to enter it, and how to link to it.

       

      Currently there's a java script (inside the html) that links to an external image file (you can search inside the file the word "HELP").


      The HTML (called: "TEST.html"):

       

      <!DOCTYPE html>

      <!--

      NOTES:

      1. All tokens are represented by '$' sign in the template.

      2. You can write your code only wherever mentioned.

      3. All occurrences of existing tokens will be replaced by their appropriate values.

      4. Blank lines will be removed automatically.

      5. Remove unnecessary comments before creating your template.

      -->

      <html>

      <head>

      <meta charset="UTF-8">

      <meta name="authoring-tool" content="Adobe_Animate_CC">

      <title>TEST</title>

      <!-- write your code here -->

      <script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>

      <script>

      (function (lib, img, cjs, ss) {

      var p; // shortcut to reference prototypes

      // library properties:

      lib.properties = {

      width: 550,

      height: 400,

      fps: 25,

      color: "#555555",

      opacity: 1.00,

      manifest: [

      {src:"images/TEST_atlas_NP_.jpg?1509437705584"/*HELP!*/, id:"TEST_atlas_NP_"}

      ]

      };

      lib.ssMetadata = [

      {name:"TEST_atlas_NP_", frames: [[0,787,320,180],[0,0,564,785]]}

      ];

      // symbols:

      (lib.cat = function() {

      this.spriteSheet = ss["TEST_atlas_NP_"];

      this.gotoAndStop(0);

      }).prototype = p = new cjs.Sprite();

      (lib.GirlImg = function() {

      this.spriteSheet = ss["TEST_atlas_NP_"];

      this.gotoAndStop(1);

      }).prototype = p = new cjs.Sprite();

      (lib.GirlImg_mc = function(mode,startPosition,loop) {

      this.initialize(mode,startPosition,loop,{});

      // Layer 1

      this.instance = new lib.GirlImg();

      this.instance.parent = this;

      this.instance.setTransform(0,0,0.34,0.34);

      this.timeline.addTween(cjs.Tween.get(this.instance).wait(1));

      }).prototype = p = new cjs.MovieClip();

      p.nominalBounds = new cjs.Rectangle(0,0,192,267.3);

      (lib.Cat_mc = function(mode,startPosition,loop) {

      this.initialize(mode,startPosition,loop,{});

      // Layer 1

      this.instance = new lib.cat();

      this.instance.parent = this;

      this.timeline.addTween(cjs.Tween.get(this.instance).wait(1));

      }).prototype = p = new cjs.MovieClip();

      p.nominalBounds = new cjs.Rectangle(0,0,320,180);

      // stage content:

      (lib.TEST = function(mode,startPosition,loop) {

      this.initialize(mode,startPosition,loop,{});

      // Imgs

      this.instance = new lib.Cat_mc();

      this.instance.parent = this;

      this.instance.setTransform(170.5,302,1,1,0,0,0,160,90);

      this.instance_1 = new lib.GirlImg_mc();

      this.instance_1.parent = this;

      this.instance_1.setTransform(435.1,155.6,1,1,0,0,0,96,133.6);

      this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.instance_1},{t:this.instance}] }).wait(1));

      }).prototype = p = new cjs.MovieClip();

      p.nominalBounds = new cjs.Rectangle(285.5,222,520.6,370.1);

      })(lib = lib||{}, images = images||{}, createjs = createjs||{}, ss = ss||{});

      var lib, images, createjs, ss;

      </script>

      <script>

      var canvas, stage, exportRoot;

      function init() {

      canvas = document.getElementById("canvas");

      images = images||{};

      ss = ss||{};

      var loader = new createjs.LoadQueue(false);

      loader.addEventListener("fileload", handleFileLoad);

      loader.addEventListener("complete", handleComplete);

      loader.loadManifest(lib.properties.manifest);

      }

      function handleFileLoad(evt) {

      if (evt.item.type == "image") { images[evt.item.id] = evt.result; }

      }

      function handleComplete(evt) {

      //This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.

      var queue = evt.target;

      var ssMetadata = lib.ssMetadata;

      for(i=0; i<ssMetadata.length; i++) {

      ss[ssMetadata[i].name] = new createjs.SpriteSheet( {"images": [queue.getResult(ssMetadata[i].name)], "frames": ssMetadata[i].frames} )

      }

      exportRoot = new lib.TEST();

      stage = new createjs.Stage(canvas);

      stage.addChild(exportRoot);

      //Registers the "tick" event listener.

      createjs.Ticker.setFPS(lib.properties.fps);

      createjs.Ticker.addEventListener("tick", stage);    

      //Code to support hidpi screens and responsive scaling.

      (function(isResp, respDim, isScale, scaleType) {

      var lastW, lastH, lastS=1;

      window.addEventListener('resize', resizeCanvas);

      resizeCanvas();

      function resizeCanvas() {

      var w = lib.properties.width, h = lib.properties.height;

      var iw = window.innerWidth, ih=window.innerHeight;

      var pRatio = window.devicePixelRatio || 1, xRatio=iw/w, yRatio=ih/h, sRatio=1;

      if(isResp) {               

      if((respDim=='width'&&lastW==iw) || (respDim=='height'&&lastH==ih)) {                   

      sRatio = lastS;               

      }

      else if(!isScale) {

      if(iw<w || ih<h)

      sRatio = Math.min(xRatio, yRatio);

      }

      else if(scaleType==1) {

      sRatio = Math.min(xRatio, yRatio);

      }

      else if(scaleType==2) {

      sRatio = Math.max(xRatio, yRatio);

      }

      }

      canvas.width = w*pRatio*sRatio;

      canvas.height = h*pRatio*sRatio;

      canvas.style.width = w*sRatio+'px';

      canvas.style.height = h*sRatio+'px';

      stage.scaleX = pRatio*sRatio;

      stage.scaleY = pRatio*sRatio;

      lastW = iw; lastH = ih; lastS = sRatio;

      }

      })(true,'height',true,1);

      }

      </script>

      <!-- write your code here -->

      </head>

      <body onload="init();" style="margin:0px;">

      <canvas id="canvas" width="550" height="400" style="display: block; background-color:rgba(85, 85, 85, 1.00)"></canvas>

      </body>

      </html>

       

       



      The Sprite Sheet (called: "TEST_atlas_NP_.jpg", originaly being exported to "image" folder, near the html file) :

      TEST_atlas_NP_.jpg

       

       

      This is the result we're searching for, once we open the html file:

       

       

      Tnx!