expo / expo-2d-context

A pure-js implementation of the W3C's Canvas-2D Context API that can be run on either Expo Graphics or WebGL
112 stars 7 forks source link

Typescript error for Expo2DContext #30

Open codeReader52 opened 2 years ago

codeReader52 commented 2 years ago

Hi experts, I was attempting to initialise the Expo2DContext according to the example in README.md (here) and got some typescript errors on both parameters in the constructor. Currently, the type specified in the constructor is: constructor(gl: number, options: Expo2dContextOptions). Specifically:

Thanks for looking at my queries!!! 🙏 🙏

natemunk commented 2 years ago

I am having the same issue. Were you able to get this resolved?

egordorichev commented 2 years ago

I've been researching this issue, looks like the gl object has contextId property, that is used for the constructor. So


var ctx = new Expo2DContext(gl.contextId, {
  ...
})
Kana00 commented 1 year ago
var ctx = new Expo2DContext(gl.contextId, {
  ...
})

This solution doesn't work in some cases when the context needs the functions in gl, it doesn't find them if you put a number... I propose an alternative that I am using for the moment, until this issue is solved

import { ExpoWebGLRenderingContext, GLView } from "expo-gl";
import Expo2DContext, { Expo2dContextOptions } from "expo-2d-context";

interface YOUR_PROPS {}

const options: Expo2dContextOptions = {
  fastFillTesselation: true,
  maxGradStops: 128,
  renderWithOffscreenBuffer: false
}

export const YOUR_COMPONENT: React.FC<YOUR_PROPS> = (props) => {

  const onGLContextCreate = (gl: ExpoWebGLRenderingContext) => {
    // @ts-ignore
    var ctx = new Expo2DContext(gl, options);

    // draw shapes
    ctx.beginPath();
    ctx.arc(40, 40, 40, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.flush();
  };

  return (
    <GLView style={{ width: 80, height: 80, backgroundColor: '#00000011' }} onContextCreate={onGLContextCreate} />
  );
};
tomdoeslinux commented 9 months ago

Something with the TypeScript code is off :/