Code Monkey home page Code Monkey logo

ruler's Introduction

Ruler

npm version React Preact Angular Vue Vue

A Ruler component that can draw grids and scroll infinitely.

Demo / API / Guides / Main Project

⚙️ Installation

npm

$ npm i @scena/ruler

scripts

<script src="//daybrush.com/ruler/release/latest/dist/ruler.min.js"></script>

🚀 How to use

import Ruler from "@scena/ruler";

const ruler = new Ruler(document.body, {
    type: "horizontal",
});


window.addEventListener("resize", () => {
    ruler.resize();
});

export interface RulerInterface {
    scroll(scrollPos: number): any;
    resize(): any;
}
export interface RulerProps {
    type?: "horizontal" | "vertical";
    width?: number;
    height?: number;
    unit?: number;
    zoom?: number;
    direction?: "start" | "end";
    style?: IObject<any>;
    backgroundColor?: string;
    lineColor?: string;
    textColor?: string;
    textFormat?: (scale: number) => string;
}

Ruler Units

The default unit is px, and a line is drawn every 50px. If you want to use a different unit instead of the px unit, use it like this:

  • 1px (Default)
    • zoom: 1
    • unit: 50 (every 50px)
  • 1cm = 37.7952px
    • zoom: 37.7952
    • unit: 1 (every 1cm)
  • 1in = 96px = 2.54cm
    • zoom: 96
    • unit: 1 (every 1in)

⚙️ Developments

The ruler repo is managed as a monorepo with yarn.

yarn config set registry https://registry.npmjs.org/

⭐️ Show Your Support

Please give a ⭐️ if this project helped you!

👏 Contributing

If you have any questions or requests or want to contribute to ruler or other packages, please write the issue or give me a Pull Request freely.

🐞 Bug Report

If you find a bug, please report to us opening a new Issue on GitHub.

📝 License

This project is MIT licensed.

MIT License

Copyright (c) 2019 Daybrush

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

ruler's People

Contributors

adam-26 avatar catrielmuller avatar clevergod avatar daybrush avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ruler's Issues

React Ruler transparent background not working

Environments

  • Framework name: React, Next Js
  • Framework version: 18.2.0, 13.4.3
  • Component name: @scena/react-ruler
  • Component version: 0.17.1
  • Testable Address(optional):

Description

I was previously using react-ruler 0.9.1 and was setting the ruler's background as transparent like so:

 <Ruler
      type="horizontal"
      unit={1}
      zoom={wholeNoteDivisions * columnWidth}
      ref={scaleGridMinor}
      backgroundColor={"rgba(0,0,0,0)"}
      segment={wholeNoteDivisions}
      mainLineSize={0}
      shortLineSize={gridHeight}
      longLineSize={gridHeight}
      height={gridHeight}
      lineColor="rgba(255,255,255,0.1)"
      textColor="rgba(0,0,0,0)"
  />

This worked well, but today I updated to react-ruler 0.17.1 and this not longer works, instead showing a black background. Has there any changes since then that makes this not possible, or am I doing something wrong in the new version?

Vertical rule grow infinite !

I think the vertical rule have some bug ? it seem grow infinitely when is compute rendering!
Horizontal work fine !

here preview gif
s4g4wheeabwsa3rf3fg3aga2552223
code used in my projet look like this
ps:i also try many css styles, without success.

const Camera = ({ children }) => {
	const pos = store({
		x: 0,
		y: 0,
	});
	return (
		<>
	<Box
			className='content'
			bgcolor='grey.700'
			display='flex'
			justifyContent='top'
			flexDirection='column'
			style={{ minWidth: '100%', width: 0, minHeight: '100%', height: 0 }}
		>
			<Box bgcolor='grey.200'>
				<div className='Ruler' style={{ width: '100%', height: '24px' }}>
					<Ruler type='horizontal' scrollPos={pos.x} />
				</div>
			</Box>
			<Box
				className='content'
				bgcolor='grey.700'
				display='flex'
				justifyContent='top'
				flexDirection='row'
				flexGrow={1}
			>
				<Box m={0.5} display='flex' bgcolor='grey.200'>
					<div className='Ruler' style={{ width: '28px' }}>
						<Ruler type='vertical' scrollPos={pos.y} />
					</div>
				</Box>
				<Box flexGrow={1} bgcolor='grey.200'>
					<TransformWrapper
						options={{
							limitToBounds: true,
							limitToWrapper: true,
							minScale: 1,
							maxScale: 2,
						}}
						onPanning={(e) => {
							pos.x = e.positionX;
							pos.y = e.positionY;
						}}
					>
						<TransformComponent>
							<div>Camera:</div>
							<div>{children}</div>
						</TransformComponent>
					</TransformWrapper>
				</Box>
			</Box>
		</Box>
		</>
	);
};
export default view(Camera);

is this a know issue ?
or if you want i can take time to make you a demo ?
thanks again

When I use cm as units. It creates long numbers with decimals.

When I use cm as units. It creates long numbers with decimals. Imo this should be rounded away.

Here is the diff that solved my problem:

diff --git a/node_modules/@scena/react-ruler/src/react-ruler/Ruler.tsx b/node_modules/@scena/react-ruler/src/react-ruler/Ruler.tsx
index 8a32e3e..946fa48 100644
--- a/node_modules/@scena/react-ruler/src/react-ruler/Ruler.tsx
+++ b/node_modules/@scena/react-ruler/src/react-ruler/Ruler.tsx
@@ -163,10 +163,10 @@ export default class Ruler extends React.PureComponent<RulerProps> implements Ru
                     ? [startPos + alignOffset * -3, isDirectionStart ? 17 : height - 17]
                     : [isDirectionStart ? 17 : width - 17, startPos + alignOffset * 3];
 
-                let text = `${value * unit}`;
+                let text = String(Math.round(value * unit));
 
                 if (textFormat) {
-                    text = textFormat(value * unit);
+                    text = textFormat(text);
                 }
 
                 context.textAlign = textAlign;

This issue body was partially generated by patch-package.

ngx-ruler type issues

Description

cannot build latest ngx-ruler, mainly because it cant find/reolve @scena/ruler
error TS2307: Cannot find module '@scena/ruler' or its corresponding type declarations.

zoom

setzoom???
No??

Percent Support

Description

Any setting for notching at percentages as opposed to pixels?

Can I change the width of the actual ruler line?

Environments

  • Framework name: React

Description

In ruler/packages/react-ruler/src/react-ruler/Ruler.tsx

There is context.lineWidth = 1; in line 134

Is there any way to change the value of lineWidth?
Didnt find it on the API.
Thanks ahead.

Zooming

react-ruler

How can implement zoom on wheel, please suggest

Option to render selectedRange values

Environments

All / latest

Description

Hey there 👋 I was wondering - how difficult would it be to add values for selectedRanges like so (see blue titles on the edge):

CleanShot 2023-01-29 at 14 54 13@2x

Maybe something like this:

  • new prop selectedRangesText={true|false}
  • new prop selectedRangesTextOffset={[x, y]} (so to maintain the same API as textOffset={[x, y]}
  • new prop selectedRangesTextColor (so to maintain the same API as textColor='#xxx'

https://github.com/daybrush/ruler/blob/master/packages/react-ruler/src/react-ruler/Ruler.tsx#L206
(I'm not familiar with drawing in canvas, but maybe it would be easy then to draw text on each side of selected range?)

Infinite Ruler Crashes Firefox when wrapped in parent and given width

Environments

  • Framework name: ReactJS
  • Framework version: 17.0.1
  • Component name: @scena/react-ruler
  • Component version: ^0.7.2
  • Not working on browser: Firefox

Description

I am using react ruler with infinite scrolling to 24000px by 24000px of page. Using both vertical and horizontal rulers with sticky position on parent element. It is working great and as expected on all browsers but firefox. Firefox gives 2 errors:

  • How it is told in the demo creating the resize function. Firefox fails to recognise that and thorws ruler.resize is not a function
  • I removed the function and still works properly on other browsers and then throws error on Ruler.tsx:111 and just crashes.

What I tried

  • I tried to remove the height and width of the parent element for the ruler and the horizontal starts working but the width is just the width of the screen and not more even though the page is way more than that.
  • By any means I am unable to have a vertical scroll bar

Need help.

How to scale a Ruler component centered on the mouse pointer position

Environments

  • Framework name: react, nodejs
  • Framework version:
  • Component name: Ruler
  • Component version:^0.12.2
  • Testable Address(optional):

Description

I perfected my designer with the Ruler component, and now I want to implement a mouse point as the center, scaling the canvas while scaling the Ruler component, when using the zoom property, the component does scale, but from the effect, the zoom center is in the upper left corner, not the coordinates of the mouse; I tried getting the ruler instance using the ref attribute and calling the TRANSLATE method of the canvas instance, but it failed because the ruler component doesn't seem to be a pure canvas object

Support for Non-Pixel Units

I would like my rulers to use a different set of units than pixels. Some examples would be:

  • inches
  • centimeters
  • millimeters

It would be great if there was a prop that allowed me to specify a pixel --> custom unit conversion. (E.g. 1 px = 1/72 inch)

The text is a little fuzzy, how to fix it?

I find the text on canvas is a little fuzzy, and I think your solution is that enlarge the canvas twice. I did this in my project too, but it's not clear at all. Is there anyway to fix this problem? thanks!!!

Сan i change the length marks?

Environments

  • Framework name: Angular
  • Framework version: 8.2
  • Component name: ruler
  • Component version: 0.2.3
  • Testable Address(optional): -

Description

Hi!
I want the horizontal ruler to have numbers on the bottom.
I want the vertical ruler to have numbers on the right.
bottom and right rulers
How can I do it?

Feature: Canvas scale option

Environments

  • Framework name: VanillaJS
  • Component name: Ruler
  • Component version: 0.12.2

Description

Allow for context.scale(x, y); to be customized.
It currently is hardcoded to context.scale(2, 2);.

I have found that changing it from 1 to 3 based on whether it is a high density screen or not to be helpful between monitors.

function isHighDensity () {
  let isHighDensity = false
  if (window.matchMedia) {
    const mq = window.matchMedia('only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen  and (min-device-pixel-ratio: 1.3), only screen and (min-resolution: 1.3dppx)')
    isHighDensity = mq && mq.matches
  }

  return isHighDensity || window.devicePixelRatio > 1
}

const rulerScale = isHighDensity() ? 3 : 1

...

context.scale(rulerScale, rulerScale);

Another thing I have found to help with the rendering is to force antialiasing of the canvas by using subpixeling. The x position of the tick can be offset by .5 + (isHorizontal ? -1 : 1) to enable it. Might be worth exploring.

Custom styling of a ruler

Environments

All?

Description

Hey, I was trying to find if there is a way to change certain style things in the ruler and couldn't find if it's possible.

CleanShot 2023-01-26 at 20 28 24@2x

Questions:

  1. How can I change ruler height to 20px without hiding numbers? ☝️
  2. Is there a way to hide those "minor" (red) ruler lines and keep only "major" (green)?
  3. Is it possible to render shorter "major" (green) line and render number above it?

I understand 3rd might be tricky, but if it's possible to do 1 and 2 would be awesome. Thank you

Desired looks would be something like this 👇
CleanShot 2023-01-26 at 20 31 15@2x

Ability to customize number (and size of) ticks?

Environment

  • Framework name: react
  • Framework version: 17.0.2
  • Component name: @scena/react-ruler
  • Component version: 0.7.3

Description

Using your suggestion, we were able to get our rulers to render in inches rather than pixels. However, the number of ticks (and size of the ticks) is not intuitive to the user, because each inch is being divided into 10ths (10 slices), rather than 8ths (8 slices) or 16ths (16 slices).

Current display:

image

Desired display:

download

Is it possible to customize the number (and size of) the ticks?

Numbers in more detail

Environments

  • Framework name:
  • Framework version:
  • Component name:
  • Component version:
  • Testable Address(optional):

Description

Hi. When I want my unit to be a huge number, ex: 1000, how do I add in-between numbers like:100, 500, etc?

Thank you

Rulers are not displaying text

Environments

  • Framework name: React.js
  • Framework version: 17.0.2
  • Component name: @scena/react-ruler
  • Component version: 0.9.1
  • Testable Address(optional):

Description

The measurements are not showing. I am using a vertical and horizontal ruler like in the image below, and the text does not show up. I tried increasing the width and height with no success.

image

Horizontal ruler:

<Ruler
	ref={horizontalRulerRef}
	type="horizontal"
	height={16}
	segment={4}
	zoom={viewZoom}
></Ruler>

Vertical ruler:

<Ruler
	type="vertical"
	width={16}
	segment={4}
	ref={verticalRulerRef}
	zoom={viewZoom}
></Ruler>

Bug with throttle drag used on group moveable

Bug with throttle drag used on group moveable

throttle drag works well when only 1 item is moveable
but when you use Group+Moveable+ throttledrag, the throttle does not work well

I tested the code also on your StoryBook, same results

throttleDrag = 25px

see this gif for the bug

PS: Amazing work!

bug_group_moveable_plus_thottle_drag

Highlight selected elements or canvas dimmensions

Environments

  • Framework name: angular
  • Framework version:
  • Component name: Ruler
  • Component version: latest
  • Testable Address(optional):

Description

How can I make something like this?
image
or this
image

how to highlight in different color selected object or canvas size

Support pnpm

Environments

Vite 4.2.1
pnpm 8.2.0
@scena/ruler 0.18.1
error:
node_modules/.pnpm/@Scena[email protected]/node_modules/@scena/ruler/dist/ruler.esm.js:11:77:
11 │ import ReactRuler, { PROPERTIES as PROPERTIES$1, METHODS as METHODS$1 } from 'react-compat-ruler';

backgroundColor, textColor and lineColor have no effect

Environments

Description

I have component Ruler
I assign values to props "backgroundColor", "textColor" and "lineColor" - this is in my code
and I pass props to template "rulers.component.html".
In the browser, the ruler does not change color, there are no errors in the console.
Why?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.