Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zoom-image-data

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zoom-image-data

zoom-image-data

  • 2.0.6
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-40%
Maintainers
1
Weekly downloads
 
Created
Source

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)
  }
}

Keywords

FAQs

Package last updated on 12 Oct 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc