1.0.0 • Published 5 months ago

scale-ruler v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

scale-ruler

A javascript public component, an excellent assistant for building a low-code platform. It supports free movement and scaling of the canvas within the area, and includes ruler, positioning line and adsorption line functions.

中文

demo

Features

  • Supports shortcut key zoom(e.g. use ctrl + '+' and ctrl + '-' to zoom) , mouse zoom and touchpad dual finger pinch zoom
  • Supports free movement and scroll bar movement
  • Easy and fast access

Installation

Using Package Manager

npm install scale-ruler --save
or
yarn add scale-ruler
# or
pnpm add scale-ruler
# or
bun add scale-ruler

Import in Browser

unpkg

<head>
  <script src="//unpkg.com/scale-ruler"></script>
</head>

jsDelivr

<head>
  <script src="//cdn.jsdelivr.net/npm/scale-ruler"></script>
</head>

Quick Start

<div id="wrap"></div>

<script>
let _scale = 0;
let adsorptionXList = [
  100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400,
  1500, 1600, 1700, 1800
];
let adsorptionYList = [100, 200, 300, 400, 500, 600, 700, 800, 900];
const opt = {
  el: '#wrap',
  containerAutoSize: true,
  autoCenter: true,
  autoScale: true,
  canvasStyle: {
    backgroundColor: '#fff'
  },
  positionLineConfig: {
    adsorptionXList,
    adsorptionYList
  },
  onScale(scale) {
    _scale = scale;
  },
  onMove(translateX, translateY) {
  },
  adsorptionLineChange(list, isY) {
    if (isY) {
      adsorptionYList = list;
    } else {
      adsorptionXList = list;
    }
    setInputValue(list, isY);
  },
  positionLineChange(list, isY) {
  }
};
const ruler = new ScaleRuler(opt);

// get canvas element
const canvasEl = ruler.getCanvasEl();
// forbid to scale the canvas
ruler.forbidScale()
// allow to scale the canvas
ruler.allowScale()
</script>

Reference code

Attributes

AttributeDescriptionTypeDefaultRemark
scaleScale ratio of canvasNumber1If autoScale is true,The initial value of scale does not take effect
minScaleThe minimum value of scale ratio of canvasNumber0.1
maxScaleThe maximum value of scale ratio of canvasNumber10
autoScaleWhether to automatically calculate the scale ratio of canvas during initializationBooleantrue
canScaleWhether scaling is allowed or notBooleantrue
autoCenterWhether to automatically center the canvas during initializationBooleantrue
containerAutoSizeWhether to automatically calculate the width and height of the containerBooleantrueIf true, it will monitor the changes of container width and height and repaint the canvas and ruler
containerWidthContainer widthNumber1000If containerAutoSize is true, this value does not take effect
containerHeightContainer heightNumber500If containerAutoSize is true, this value does not take effect
containerXPaddingThe left and right padding between the container and the canvas in the x-direction/horizontal directionNumber800
containerYPaddingThe top and bottom padding between the container and the canvas in the y-direction/vertical directionNumber800
canvasWidthCanvas widthNumber1920
canvasHeightCanvas heightNumber1000
canvasStyleThe style of canvasObject{}
proxyScaleKeyWhether to proxy the shortcut key zoom function or notBooleantrueproxy ctrl+ "+" to zoom in and ctrl + "-" to zoom out
useScrollBarWhether to use scrollbarBooleantrue
scrollBarConfigThe config of scrollbarObject{}see scrollBarConfig params
useRulerWhether to use rulerBooleantrue
rulerConfigThe config of rulerObject{}see rulerConfig params
usePositionLineWhether to use position lineBooleantrue
positionLineConfigThe config of position lineObject{}see positionLineConfig params

scrollBarConfig params

AttributeDescriptionTypeDefaultRemark
bgColorbackground colorString#000000
opacityopacityNumber0.4
zIndexzIndexNumber500
sizeThe height of horizontal scrollbar and the width of vertical scrollbarNumber8

rulerConfig params

AttributeDescriptionTypeDefaultRemark
xRulerHeightThe height of x/horizontal rulerNumber30Do not bigger than the containerYPadding
yRulerWidthThe width of y/vertical rulerNumber30Do not bigger than containerXPadding
bgColorBackground color of rulerString#efefef
fontColorThe color of number in rulerString#000000
fontSizeThe font size of number in rulerNumber12
fontFamilyThe font family of number in rulerStringArial
lineColorThe line color of rulerString#000000
zIndexzIndexNumber400

positionLineConfig params

AttributeDescriptionTypeDefaultRemark
lineColorThe color of position lineString#24aa61
paddingThe padding of operationNumber3
adsorptionXListThe list of adsorption in x/horizontal directionArray0, canvasWidth
adsorptionYListThe list of adsorption in y/vertical directionArray0, canvasHeight
adsorptionGapThe gap to adsorbNumber4
zIndexzIndexNumber300

Events

Methods NameDescriptionParameters
onScaletriggers when scaling the canvasscale: number
onMovetriggers when moving the canvastranslateX: number, translateY: number
adsorptionLineChangetriggers when adsorption line(s) change(s)value: number[], isY: boolean
positionLineChangetriggers when position line(s) change(s)value: number[], isY: boolean

Methods

Methods NameDescriptionParameters
resetreset the canvas-
changeScalechange the scale ratio of canvas(scale:number)
removeAllPositionLineremove all the position line(s)-
showRulershow rulers-
hideRulerhide rulers-
showAllPositionLineshow all the position line(s)-
hideAllPositionLinehide all the position line(s)-
addAdsorptionLineadd adsorption line(s)(data: number | Array- the value of adsorption line, isY: boolean -Whether it is in the y/vertical direction)
removeAdsorptionLineremove adsorption line(s)(data: number | Array- the value of adsorption line, isY: boolean -Whether it is in the y/vertical direction)
forbidScaleforbid to scale the canvas-
allowScaleallow to scale the canvas-
getCanvasElget the canvas element-
1.0.0

5 months ago