Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #34356

Re: Object.create(canvas.getContext())

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Object.create(canvas.getContext())
Date 2017-11-15 02:32 +0100
Organization PointedEars Software (PES)
Message-ID <1754058.usQuhbGJ8B@PointedEars.de> (permalink)
References <ou96ju$mvd$1@gwaiyur.mb-net.net> <5326256.lOV4Wx5bFT@PointedEars.de> <ouel6d$nvu$1@gwaiyur.mb-net.net>

Show all headers | View raw


Jan Bruns wrote:

> Thomas 'PointedEars' Lahn:
>> […] this is particulary true of host objects as this one.  If subclassing
>> does not work either (or cannot be assumed to be available), create a
>> wrapper object.
> 
> That's what I wanted to avoid.
> 
> Open GL, GL ES, and webgl are all APIs that hide obejct oriented
> programming aspects by some state mechanism. For example, to change some
> object's property often means to first set the object reference to some
> "current object" state using some API method, to finally change the
> property of that "current object" using some other API method.
> 
> This is beacause initial OpenGL was focused on plain C usage, I guess.

WebGL is an *object-oriented* API based on OpenGL ES 2.0, so I do not see 
your point.
 
> Wrapping all the webgl methods explicitly just to get some functionality
> that the prototype chain and the API's host code could do doesn't sound
> very attractive either, so I'll decide to put the burden to distungish
> native API calls from helper API calls on some higher level helper API.

Your choice.  I see that subclassing does not work in this case.

But it appears to be rather easy to create such a wrapper, at least in 
Chromium 57:

  var my = (function () {
    var canvas = document.createElement("canvas");
    var context = canvas.getContext("webgl");
    var proto = Object.getPrototypeOf(context);

    function WebGLWrapper (context)
    {
      this.context = context;
    }

    /* Wrap built-in methods */
    Object.assign(WebGLWrapper.prototype, (function () {
      var wrappedMethods = Object.create(null);

      // Object.keys(proto).forEach(function (key) {
      Object.keys(proto).forEach((key) => {
        try
        {
          if (typeof proto[key] == "function")
          {
            wrappedMethods[key] = (function (freeKey) {
              return function (...args) {
                // this.context[freeKey].apply(this.context, arguments);
                return this.context[freeKey](...args);
              };
            }(key));
          }
        }
        catch (e) {}
      });

      return wrappedMethods;
    }()));

    Object.assign(WebGLWrapper.prototype, {
      /* custom methods */
    });

    return {
      /** @memberOf my */
      WebGLWrapper: WebGLWrapper
    };
  }());

  // gl = new my.WebGLWrapper(gl);

Barely tested.
 
>> If available, you can use “Proxy” (ECMAScript 2015+) or
>> “__noSuchMethod__” (Mozilla JavaScript 43+ only, obsolete) to implement
>> seamless access.
> 
> Didn't know about that, but currently looks like much of browser
> specifics

WebGL is browser-specific.  “Proxy” is not, in general.  However, Internet 
Explorer supports WebGL, but its JScript version does not implement either 
“Proxy” or “__noSuchMethod__”, which might be a showstopper for that 
approach.

> that don't have anything to do with webgl.

Yes, it is a general approach.  Your point being?

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Object.create(canvas.getContext()) Jan Bruns <ebay@abnuto.de> - 2017-11-12 10:09 +0000
  Re: Object.create(canvas.getContext()) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-12 17:42 +0100
    Re: Object.create(canvas.getContext()) Jan Bruns <ebay@abnuto.de> - 2017-11-14 11:48 +0000
      Re: Object.create(canvas.getContext()) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-15 02:32 +0100
        Re: Object.create(canvas.getContext()) Jan Bruns <ebay@abnuto.de> - 2017-11-17 20:25 +0000
          Re: Object.create(canvas.getContext()) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-19 05:48 +0100
            Re: Object.create(canvas.getContext()) Jan Bruns <ebay@abnuto.de> - 2017-11-19 21:39 +0000
              Re: Object.create(canvas.getContext()) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-19 23:34 +0100
                Re: Object.create(canvas.getContext()) Jan Bruns <ebay@abnuto.de> - 2017-11-20 14:47 +0000
                Re: Object.create(canvas.getContext()) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-21 04:07 +0100
                Re: Object.create(canvas.getContext()) Jan Bruns <ebay@abnuto.de> - 2017-11-21 16:23 +0000
                Re: Object.create(canvas.getContext()) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-22 04:12 +0100
  Re: Object.create(canvas.getContext()) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-12 18:45 +0100

csiph-web