[r114] Could not getContext from c2canvas element

0 favourites
  • 6 posts
From the Asset Store
🙌 Element Magic Hit Sound Pack comes with 805 high-quality sound effect
  • I wrote a plugin to lazy load image from URL to prevent broken image in case of slow network..

    Everything are fine in r95. But in r114 my javascript code is broken..??

    Following this code:

    ///

    var c=document.getElementById("c2canvas");

    var ctx=c.getContext("2d");

    /////

    And the answer is ctx == null or undefined

    This is bug...???

    My code segment here

    <font color=RED>

    // draw myImage

         instanceProto.paint = function (myparam)

         {

          //alert("here: "+ this.runtime.id);      

          //alert(this.x);

          var _x = this.x;

          var _y = this.y;

          var _w = this.width;

          var _h = this.height;

            //this.runtime.trigger(pluginProto.cnds.ImageLoaded,instance);

            var image = new Image();

            image.src = myparam;           

           var c=document.getElementById("c2canvas");

           //var c = $("#c2canvas")[0>;        var ctx=c.getContext("2d");        if(c.getContext("2d")!=null){                image.onload= function(){                    ctx.drawImage(image, _x - _w/2, _y- _h/2, _w,_h);         }            //this.draw(con);            //this.runtime.redraw=true;         this.image = image;                }else{             alert("what the heck...??");         }      };      ////////////////////////////////////// So how do I solve this problem in case the runtime was changed...??? Thank in advanced

  • This code should never have worked. You can't call getContext on c2canvas, because it's already called that and you can only call it once. Further the c2canvas can also be a WebGL context if WebGL is supported.

    Why not just use Sprite's 'Load image from URL'?

  • Dear Asley..thank for your reply...

    Actually in R95 my code work very fine....

    I cannot using the LoadImage from URL in sprite because this feature is working very bad in case of large image or low bandwidth work.

    It is meaning why I have to write my own plugin to get image.

    In my game...player have it own Avatar and sprite doesn't redraw image after it is loaded already...

    I just looking for solution for this problem...

    Hope you have some suggestion for me in these cases...

    Thank you very much.

  • Your code is definitely wrong, it must have worked by accident in r95.

  • Dear Ashley

    So I believe that there are something wrong with LoadImage from URL of Sprite...So sorry because I don't know how to post capx file...:) but steps of reproduced this bug like that

    System->OnStartLayout: Load image from URL ...."www.something.com"

    Button->OnClick: Create Sprite then Load image from URL ...."www.somethingelse.com"

    Result: Both image same from something.com

    Expected: Images must be different

    Thank you very much...

    PS: this is reason I can't using LoadImage from URL of Sprite component..

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • For more information

    The sprite at initial state have no image..

    I am going to use LoadImageFromURL in case above problem solved...

    Thank you very much...loy@Ashley

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)