123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- export function drawScaleplate({
- canvas,
- canvasStyleWidth,
- canvasStyleHeight,
- direcotion,
- scale,
- scrollX,
- scrollY,
- originX,
- originY,
- }: {
- canvas: HTMLCanvasElement;
- direcotion: "horizontal" | "vertical";
- canvasStyleWidth: number;
- canvasStyleHeight: number;
- scale: number;
- scrollX: number;
- scrollY: number;
- originX: number;
- originY: number;
- }) {
- const ctx = canvas.getContext("2d");
- if (!ctx) return;
- ctx.clearRect(0, 0, canvas.width, canvas.height);
-
- const drp = window.devicePixelRatio || 1;
- const width = (canvas.width = canvasStyleWidth * drp);
- const height = (canvas.height = canvasStyleHeight * drp);
-
- const hStartNum = (scrollX - originX) / scale;
- const vStartNum = (scrollY - originY) / scale;
-
- const tickSpacingOptions = [20, 50, 100, 250, 500, 1000];
- const maxTickNum = tickSpacingOptions.reduce((prev, curr) => {
- return Math.abs(curr - 100 / scale) < Math.abs(prev - 100 / scale)
- ? curr
- : prev;
- });
-
- const minTickNum = maxTickNum / 10;
-
- const minTickSpacing = (maxTickNum / 10) * scale * drp;
- const maxLength = direcotion === "horizontal" ? width : height;
-
- let startNum = Math.round(
- direcotion === "horizontal" ? hStartNum : vStartNum
- );
-
- let startTickOffset = 0;
- if (startNum % minTickNum !== 0) {
- startTickOffset += Math.abs((startNum % minTickNum)) * scale * drp;
- startNum -= startNum % minTickNum;
- }
-
- for (
- let tickSpacing = startTickOffset;
- tickSpacing < maxLength;
- tickSpacing += minTickSpacing
- ) {
-
- if(startNum % maxTickNum === 0) {
- drawMaxTick(
- ctx,
- direcotion === "horizontal"
- ? tickSpacing
- : 0,
- direcotion === "horizontal"
- ? 0
- : tickSpacing,
- startNum,
- direcotion
- );
- } else if(startNum % minTickNum === 0) {
-
- drawMinTick(
- ctx,
- direcotion === "horizontal"
- ? tickSpacing
- : 0,
- direcotion === "horizontal"
- ? 0
- : tickSpacing,
- direcotion
- );
- }
- startNum += minTickNum;
- }
- }
- function drawMaxTick(
- ctx: CanvasRenderingContext2D,
- x: number,
- y: number,
- num: number,
- direcotion: "horizontal" | "vertical"
- ) {
- const drp = window.devicePixelRatio || 1;
- ctx.beginPath();
- if (direcotion === "horizontal") {
- ctx.moveTo(x + 1, 2);
- ctx.lineTo(x + 1, y + 20 * drp);
- } else {
- ctx.moveTo(x + 1, y);
- ctx.lineTo(x + 20 * drp, y);
- }
- ctx.strokeStyle = "#666";
- ctx.lineWidth = 1 * drp;
- ctx.stroke();
- ctx.fillStyle = "#666";
- ctx.font = "16px Arial";
- if (direcotion === "horizontal") {
- ctx.fillText(num.toString(), x + 5, 10 * drp);
- } else {
- ctx.save();
- if(num / 10 >= 100) {
-
- ctx.translate(8, y + 25 * drp);
- } else if(num / 10 >= 10) {
-
- ctx.translate(8, y + 20 * drp);
- } else if(num / 10 >= 1) {
-
- ctx.translate(8, y + 20 * drp);
- } else if(num / 10 < 0) {
- ctx.translate(8, y + 25 * drp);
- } else {
- ctx.translate(8, y + 10 * drp);
- }
- ctx.rotate(-Math.PI / 2);
- ctx.fillText(num.toString(), 0, 10);
- ctx.restore();
- }
- }
- function drawMinTick(
- ctx: CanvasRenderingContext2D,
- x: number,
- y: number,
- direcotion: "horizontal" | "vertical"
- ) {
- const drp = window.devicePixelRatio || 1;
- ctx.beginPath();
- if (direcotion === "horizontal") {
- ctx.moveTo(x + 1, 12 * drp);
- ctx.lineTo(x + 1, y + 20 * drp);
- } else {
- ctx.moveTo(12 * drp, y);
- ctx.lineTo(x + 20 * drp, y);
- }
- ctx.strokeStyle = "#666";
- ctx.lineWidth = 1 * drp;
- ctx.stroke();
- }
|