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

zoom-image-data

v2.0.6

Published

zoom-image-data

Downloads

22

Readme

zoom-image-data

将指定宽高的图像缓冲区数据输出到另一宽高的图像缓冲区

1. 安装

npm install zoom-image-data

2. 构造

new zoomImageData(
  param: {
    // 输入宽
    inw: number,
    // 输入高
    inh: number,
    // 输出宽
    outw: number,
    // 输出高
    outh: number,
    // 输出缓冲区单位像素宽高比;默认值:1
    cellRatio?: number,
    // 输出缓冲区单位像素对应值;默认值:10
    cellWH?: number,
    // 是否显示分割线;默认值:false
    splitCell?: number,
    // 显示分割线时输出缓冲区对应的像素值;默认值:2
    splitCellSize?: number,
    // 输入图像缓冲区
    inData: ArrayBuffer,
    // 输出图像缓冲区
    outData: ArrayBuffer,
  }
) => zoomImageData

3. API

  • 平移
/**
 * 平移
 * @param silent 是否更新outData缓冲区
 */
translate(dx: number, dy: number, silent?: boolean) => void
  • 缩放;以(cx,cy)为中心缩放ratio比例
/**
 * 平移
 * @param silent 是否更新outData缓冲区
 */
zoom(cx: number, cy: number, ratio: number, silent?: boolean) => void
  • 将输入数据完整放置于输出窗口的正中间
/**
 * 平移
 * @param silent 是否更新outData缓冲区
 */
resize(silent?: boolean) => void
  • 更新输入->输出矩阵
updateMatrix(scale: number, dx: number, dy: number) => void
  • 坐标(x,y)是否位于输入视框内
inCoorIsIn(x: number, y: number) => boolean
  • 坐标(x,y)是否位于输出视框内
outCoorIsIn(x: number, y: number) => boolean
  • 将输入坐标组(x,y,...)转化为输出坐标组
transInToOut(coors: number[]) => number[]
  • 将输出坐标组(x,y,...)转化为输入坐标组
transOutToIn(coors: number[]) => number[]
  • 更新outData缓冲区
update() => number[]

4. 与transform-image-data 的区别

zoom-image-data单元像素可以不等比转移到输出图像缓冲区中,且支持放大到一定倍率之后显示分割线

5. 使用

以下为将一个原宽高为64*64的图像按宽高比为2绘制到指定宽高canvas元素。示例网站

html
<canvas id="canvas"></canvas>
css
html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}
javascript
import { zoomImageData } from 'zoom-image-data';
window.onload = function () {
  const canvas = document.getElementById('canvas');
  const inw = 64;
  const inh = 64;
  const inData = new ArrayBuffer(inw * inh * 4);
  const outw = document.body.clientWidth;
  const outh = document.body.clientHeight;
  const outImageData = new ImageData(outw, outh);
  canvas.width = outw;
  canvas.height = outh;
  const ctx = canvas.getContext('2d');
  const transferImageData = new ZoomImageData({
    inw,
    inh,
    inData,
    outw,
    outh,
    outData: outImageData.data.buffer,
    cellRatio: 2,
    cellWH: 4,
    splitCell: true,
    splitCellSize: 10,
  })
  
  let isDirty = true;
  fillInData();
  requestAnimationFrame(update);
  /**
   * 更新画布
   */
  function update() {
    requestAnimationFrame(update);
    if (!isDirty) return;
    isDirty = false
    transferImageData.update();
    ctx.putImageData(outImageData, 0, 0);
  }

  /**
   * 初始化inData
   */
  function fillInData() {
    const { inw, inh, inData } = transferImageData;
    for (let y = 0; y < inh; y++) {
      const s = 4291611705 - y * 512;
      for (let x = 0; x < inw; x++) {
        inData[y * inw + x] = s + x * 3;
      }
    }
  }

  /**
   * 获取鼠标相对于canvas的坐标
   */
  function getPosition(e) {
    let rect = canvas.getBoundingClientRect();
    return [e.pageX - rect.left, e.pageY - rect.top];
  }

  /**
   * 重置
   */
  document.addEventListener('keyup', function (e) {
    transferImageData.resize();
    isDirty = true;
  })

  /**
   * 缩放
   */
  canvas.addEventListener('wheel', function (e) {
    const coor = getPosition(e);
    transferImageData.zoom(coor[0], coor[1], e.deltaY < 0 ? 1.1 : 0.9);
    isDirty = true;
  })

  /**
   * 平移
   */
  let pos;
  canvas.addEventListener('mousedown', function (e) {
    pos = getPosition(e);
    document.addEventListener('mousemove', mousemove)
    document.addEventListener('mouseup', mouseup)
  })
  function mousemove(e) {
    const cur = getPosition(e);
    transferImageData.translate(cur[0] - pos[0], cur[1] - pos[1]);
    pos = cur;
    isDirty = true;
  }
  function mouseup() {
    document.removeEventListener('mousemove', mousemove)
    document.removeEventListener('mouseup', mouseup)
  }
}