webgl_helper 0.4.0+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 49

webgl_helper #

A small helper library to reduce boilerplate code when using the WebGL API.

Features #

The development of this library has just started. Many features are not completely implemented yet or missing at all.

  • Reduces boilerplate code, e.g. connecting buffers to vertex attrib locations and draw in 1 call.
  • Makes the WebGL API more type safe by adding types for a number of WebGL constants.

This library is not a high level library like three.js.

Version 0.4.0+2 #

  • Improved pub.dev score.

Version 0.4.0+1 #

  • Improved pub.dev score.

Version 0.4.0 #

  • Added support for Dart 2.

Version 0.3.0 #

  • Added support for Dart strong mode.

example/main.dart

import 'dart:html';
import 'package:webgl_helper/webgl_helper.dart';

ShaderProgram program;
Buffer buffer;

main() {
  // query the canvas element
  var canvasElement = querySelector('#webgl-canvas');

  // init the webgl rendering context
  glInit(canvasElement);

  // set the color used to clear the color buffer
  glClearColor(0, 0, 0, 1);

  // enable blending and set the blend function
  glEnableBlend(true);
  glBlendFunc(BlendingCoefficient.SRC_ALPHA, BlendingCoefficient.ONE_MINUS_SRC_ALPHA);

  // create a shader program
  program = new ShaderProgram.fromSources(vertexShader, fragmentShader);

  // create a buffer that contains 2 types of per vertex data: positions and colors
  buffer = new Buffer(BufferTarget.ARRAY_BUFFER);
  buffer.data({
    'a_position': {
      Buffer.SIZE: 2, // 2 components per vertex (x, y)
      Buffer.DATA: [1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0]
    },
    'a_color': {
      Buffer.SIZE: 3, // 3 components per vertex (r, g, b)
      Buffer.DATA: [1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0]
    }
  });

  // start rendering
  window.requestAnimationFrame(render);
}

render(tick) {
  // clear the color buffer
  glClear([ClearMode.COLOR_BUFFER]);

  // draw the vertices using the given shader program and the vertex data stored in the buffer. additionally, set an
  // uniform.
  gluDrawArrays(program, buffer, uniformSetters: {
    'u_alpha': (uniform) => uniform.set1f(0.5)
  });

  // next frame
  window.requestAnimationFrame(render);
}

// the vertex shader...
final vertexShader = '''
#version 100

precision mediump float;

attribute vec2 a_position;
attribute vec3 a_color;
varying vec3 v_color;

void main() {
  v_color = a_color;
  gl_Position = vec4(a_position / 2.0, 0.0, 1.0);
}
''';

// ...and the fragment shader
final fragmentShader = '''
#version 100

precision mediump float;

uniform float u_alpha;
varying vec3 v_color;

void main() {
  gl_FragColor = vec4(v_color, u_alpha);
}
''';

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  webgl_helper: ^0.4.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:webgl_helper/webgl_helper.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
49
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.9+1

Analysis suggestions

Package not compatible with runtime flutter-native of android

Because of the import of dart:html via the import chain package:webgl_helper/webgl_helper.dart->dart:html

Package not compatible with runtime flutter-native of ios

Because of the import of dart:html via the import chain package:webgl_helper/webgl_helper.dart->dart:html

Package not compatible with runtime flutter-native of linux

Because of the import of dart:html via the import chain package:webgl_helper/webgl_helper.dart->dart:html

Package not compatible with runtime flutter-native of macos

Because of the import of dart:html via the import chain package:webgl_helper/webgl_helper.dart->dart:html

Package not compatible with runtime flutter-native of windows

Because of the import of dart:html via the import chain package:webgl_helper/webgl_helper.dart->dart:html

Package not compatible with runtime native-aot

Because of the import of dart:html via the import chain package:webgl_helper/webgl_helper.dart->dart:html

Package not compatible with runtime native-jit

Because of the import of dart:html via the import chain package:webgl_helper/webgl_helper.dart->dart:html

Health issues and suggestions

Document public APIs. (-1 points)

159 out of 159 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/constants.dart. (-1 points)

Analysis of lib/src/constants.dart reported 2 hints:

line 137 col 7: Avoid const keyword.

line 140 col 7: Avoid const keyword.

Fix lib/src/framebuffer.dart. (-0.50 points)

Analysis of lib/src/framebuffer.dart reported 1 hint:

line 32 col 23: Don't explicitly initialize variables to null.

Format lib/src/buffer.dart.

Run dartfmt to format lib/src/buffer.dart.

Fix additional 5 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/context.dart (Run dartfmt to format lib/src/context.dart.)
  • lib/src/shader.dart (Run dartfmt to format lib/src/shader.dart.)
  • lib/src/texture.dart (Run dartfmt to format lib/src/texture.dart.)
  • lib/src/vertexattrib.dart (Run dartfmt to format lib/src/vertexattrib.dart.)
  • lib/webgl_helper.dart (Run dartfmt to format lib/webgl_helper.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.69.0 <3.0.0
vector_math ^2.0.8 2.0.8
Dev dependencies
build_runner ^0.9.2
build_web_compilers ^0.4.1