Socket
Socket
Sign inDemoInstall

@noise-machines/pixel-matrix

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @noise-machines/pixel-matrix

An ES nano-module for working with pixel data.


Version published
Maintainers
1
Created

Readme

Source

Pixel Matrix

An eloquent ES6 nano-module for working with pixel data.

Install

$ npm install pixel-matrix

Usage

⚠️ PixelMatrix is an ES module, so make sure you're using it with a transpiler or an environment that supports them. ⚠️

Convert an image to greyscale

import PixelMatrix from 'pixel-matrix'

// Read pixels from a canvas element
const canvas = document.querySelector('canvas')
canvas.style.imageRendering = 'pixelated'
const pixelMatrix = PixelMatrix.fromCanvas(canvas)

// Convert to greyscale
const greyscale = pixel => {
  const average = (pixel.red + pixel.green + pixel.blue) / 3
  return {
    red: average,
    blue: average,
    green: average,
    alpha: pixel.alpha
  }
}
const greyscalePixelMatrix = pixels.map(greyscale)

// Write pixels to canvas
greyscalePixelMatrix.putPixels(canvas)

Create a gradient

import PixelMatrix from './index.js'

// Create canvas
const canvas = document.createElement('canvas')
canvas.width = 16
canvas.height = 16
canvas.style.imageRendering = 'pixelated'
canvas.style.width = '500px'
canvas.style.height = '500px'
canvas.style.margin = 'auto'
document.body.appendChild(canvas)

// Initialize an empty 16 x 16 pixel matrix
const emptyPixelMatrix = PixelMatrix.fromCanvas(canvas)

// normalizedMap normalizes point.x and point.y to [0, 1] before passing them to
// the callback.
const gradient = emptyPixelMatrix.normalizedMap(point => {
  return {
    red: point.x * 255,
    green: 0,
    blue: point.y * 255,
    alpha: 255
  }
})

// Write pixels to canvas
gradient.putPixels(canvas)

FAQs

Last updated on 17 Apr 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc