npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

arraybufferview

v0.1.1

Published

Provides a low-level interface for reading and writing structured data in a binary ArrayBuffer.

Downloads

1

Readme

ArrayBufferView

Provides a low-level interface for reading and writing structured data in a binary ArrayBuffer.

Installation

From NPM:

import ArrayBufferView from 'arraybufferview';

From CDN:

<script src="http://unpkg.com/arraybufferview/dist/arraybufferview.js"></script>

Examples

You can create a buffer and fill it with structed data.

const struct = {
  a: 1,
  b: 5,
  c: 2,
};

const buffer = new ArrayBuffer(100);

const view = new ArrayBufferView(struct, buffer);

view.fill({a: 1, b: 12, c: 3});

const data0 = view.get(0);

console.log(data0.b); // 12
console.log(data0.c); // 3

Use ArrayBufferView to manipulate binary data such as blob or image data.

function loadImage(url) {
  const img = new Image();
  img.crossOrigin = 'anonymous';
  return new Promise((resolve) => {
    img.onload = function () {
      resolve(img);
    };
    img.src = url;
  });
}

(async function () {
  const src = 'https://p1.ssl.qhimg.com/t011f60e5399df3d7a6.png';
  const img = await loadImage(src);

  const canvas = document.getElementById('app');
  const context = canvas.getContext('2d');

  context.drawImage(img, 0, 0, img.width, img.height);

  const imageData = context.getImageData(0, 0, img.width, img.height);
  const buffer = imageData.data.buffer;

  const struct = {
    r: 8,
    g: 8,
    b: 8,
    a: 8,
  };

  const view = new ArrayBufferView(struct, buffer);
  
  // make image color black an white
  view.forEach(({r, g, b, a}, i, view) => {
    view.set(i, {
      r: (r + g + b) / 3,
      g: (r + g + b) / 3,
      b: (r + g + b) / 3,
      a,
    });
  });

  context.putImageData(imageData, 0, 0);
}());

Api

constructor(structure, arrayBufferOrLength)

The constructor of the ArrayBufferView.

  • The structure is a metadata of the ArrayBufferView.
const structure = {
  a: 1, // property a is 1 bitwidth.
  b: 3, // property b is 3 bitwidth.
  c: 4, // property c is 4 bitwidth,
  d: Number,  // property d is a number type
  e: Boolean, // property e is a boolean type
}

// create an array buffer with 1000 structured data items.
const view = new ArrayBufferView(structure, 1000);

set(index, data)

Set data to ArrayBufferView.

view.set(2, {
  a: 0,
  c: 0xF,
  d: 3.14,
  e: true,
})

get(index)

Get data from ArrayBufferView.

forEach(callback)

The forEach() method executes a provided function once for each structured data blocks.

const view = new ArrayBufferView(structure, 1000);

view.forEach((data, index, view) => {
  data.x *= 2;
  view.set(index, data);
});

fill(data)

Fill each structed data blocks with specified data.

const buffer = imageData.data.buffer;

const struct = {
  r: 8,
  g: 8,
  b: 8,
  a: 8,
};

const view = new ArrayBufferView(struct, buffer);
view.fill({r: 0}); // red channel set to zero.

License

MIT