5
头图

Preface

Front-end colleagues who have done web-side editors will more or less come into contact with ruler plug-ins, similar to plug-ins in software such as ps or PPT.

Older web plug-ins such as jqury will generate a lot of dom, so I wrote a ruler drawn with pure TS and canvas, and does not rely on any third-party libraries. The usage method of the document is as follows, welcome to use, contribute and raise issue!

PS: The auxiliary line function is not supported yet, it will be launched as soon as possible

The plug-in also refers to another Daniel's ruler plug-in daybrush/ruler

light-ruler

oBz0Qe.png

DEMO / Github

Main features (Features)

  • Use canvas to draw, support infinite scrolling, will not generate multiple DOMs and cause page redraw
  • Support custom ruler background color, text color, scale color and unit
  • Support the translate mode, that is, after the ruler is drawn on the canvas for the first time, scrolling is achieved through css transform
  • Written in Typescript, does not rely on any third-party libraries, and the packaged file is only 26kb (including styles)
  • Support zoom scale value
  • Currently provides 2 ruler theme styles
  • Provides a multi-instance controller to manage multiple scale instances

Installation


npm i light-ruler

Useage


  • Basic use
import LightRuler from "light-ruler";

const ruler = new LightRuler({
    mode: "infinite",
    wrapperElement: document.getElementById("box"),
    scrollElement: document.getElementById("wrap"),
    rulerId: "my-ruler",
    width: 30000,
    height: 30000,
    style: {
        mode: "right",
    },
    onScroll: (x, y) => {
        console.log(x, y);
    },
});
  • React use
import React, { useRef, useEffect } from "react";
import LightRuler from "light-ruler";

export default function () {
    const rulerRef = useRef(null);

    useEffect(() => {
        const ruler = new LightRuler({
            mode: "infinite",
            mountRef: rulerRef.current,
            scrollElement: document.getElementById("wrap"),
            rulerId: "ruler",
            width: 30000,
            height: 30000,
            onScroll: (x, y) => {
                console.log(x, y);
            },
        });
    }, []);
    return (
        <div id="root">
            <div id="box">
                <div id="wrap">...</div>
                <div id="ruler" ref={rulerRef}></div>
            </div>
        </div>
    );
}
  • Vue3 use
<template>
    <div id="gomi-homePage">
        <div
            id="box"
            :style="{ position: 'relative', width: '800px', height: '600px', overflow: 'hidden', background: 'red' }"
        >
            <div id="s" :style="{ width: '100%', height: '100%', overflow: 'auto' }">
                <div id="wrap" :style="{ width: '30000px', height: '4600px' }"></div>
            </div>
            <div id="ruler" ref="ruler"></div>
        </div>
        <footer>
        </footer>
    </div>
</template>

<script lang="ts">
import LightRuler from 'light-ruler';
import { onMounted, ref, defineComponent } from "vue";

export default defineComponent({
    name: "Home",
    props: {},
    setup: () => {
        const ruler = ref(null);
        onMounted(() => {
            const myRuler = new LightRuler({
                mountRef: ruler.value,
                mode: "infinite",
                scrollElement: "#s",
                rulerId: "hh",
                width: 30000,
                height: 30000,
                style: {
                    mode: 'right'
                },
                onScroll: (x, y) => {
                    console.log(x, y);
                },
            })
        });

        return {  ruler };
    },
});
</script>

<style scoped lang="scss">
</style>
Tips: Because the ruler uses position: absolute, the container that wraps the ruler must set the position property.
At the same time, the ruler should be fixed, and the element that monitors the rolling cannot be the element that wraps the ruler.

Configuration (Config)


namemeaningvalue
mode?Drawing mode'infinite'/'translate'/'auto'
mountRef?DOM mounted by the ruler (prior to wrapperElement, the parentElement of mountRef will be used as the parent container)HTMLElement
wrapperElement?The ruler's parent container DOM (the ruler DOM will be automatically generated)HTMLElement / CSSSelector
scrollElement?Monitor scrolling DOMHTMLElement / CSSSelector
width?Ruler drawing widthnumber
height?Ruler drawing heightnumber
rulerId?Ruler idstring
style?Ruler styleObject
onScroll?Scroll callback function(x: number, y: number) => Function
  • style attribute
namemeaningvalue
size?Ruler size (such as 20, the height of the horizontal ruler is 20px, and the width of the vertical ruler is 20px)number
backgroundColor?Ruler background colorstring
fontColor?Ruler font colorstring
fontSize?Ruler font size (if not set, the appropriate size will be automatically calculated, if set, it will be an absolute value and will not be adaptive)number
fontWeight?Ruler font weight'bold'/ number
tickColor?Ruler scale colorstring
unit?Ruler unit styleObject
gap?Ruler scale intervalnumber
scale?Scale factor for scale valuenumber
show?Whether the ruler is displayed after initializationboolean
mode?Ruler theme style'center'/'right'
  • unit attribute
namemeaningvalue
backgroundColor?Unit background colorstring
fontColor?Unit font colorstring
fontSize?Unit font sizenumber
text?Unit display contentstring

API


scale
Set the scale factor of the scale, the scale value of the scale will change according to the scale factor

params

namemeaningvalue
scaleNumberZoom factornumber
ruler.scale(0.5);
resize
Reset ruler width, height or size

params

namemeaningvalue
width?Ruler drawing widthnumber
height?Ruler drawing heightnumber
size?Ruler sizenumber
ruler.resize({
    width: 1920,
    height: 1080,
    size: 18,
});
update
Update ruler style

params

namemeaningvalue
style?Ruler styleObject
ruler.update({
    fontColor: "#fff",
    unit: {
        text: "mm",
    },
});
changeScrollElement
Change the scroll object monitored by the ruler

params

namemeaningvalue
scrollElementScrolling object to monitorHTMLElement/CSSSelector
ruler.changeScrollElement("#wrap");
show
Show ruler
ruler.show();
hide
Hide ruler
ruler.hide();
destroy
Clear ruler
ruler.destroy();

Gomi
2.4k 声望2.3k 粉丝

前端,业余PPT设计师