Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default new class {
- grid(canvas = false, removeOld = true) {
- if (removeOld) {
- canvas.getObjects().forEach(g => g._isGrid ? canvas.remove(g) : '');
- }
- const width = canvas.width / canvas.getZoom();
- const height = canvas.height / canvas.getZoom();
- const step = 30;
- const lines = [];
- const addLine = (i, to) => {
- let coords = [i * step, 0, i * step, height];
- if (to == 'y') {
- coords = [0, i * step, width, i * step];
- }
- lines.push(new fabric.Line(coords, {
- fill: '#000000',
- stroke: '#000000',
- strokeWidth: 1,
- selectable: false,
- evented: false,
- }));
- };
- new Array(Math.ceil(width / step)).fill(null).forEach((v, i) => addLine(i));
- new Array(Math.ceil(height / step)).fill(null).forEach((v, i) => addLine(i, 'y'));
- const group = new fabric.Group(lines, {
- selectable: false,
- evented: false,
- _isGrid: true,
- originX: 'left',
- originY: 'top'
- });
- if (this.gridObject) { canvas.remove(this.gridObject); }
- this.gridObject = group;
- this.gridObject._isGrid = true;
- return this.gridObject;
- }
- ruler(canvas = false, zoomOrigin = 1, removeOld = true, color = '#55606c') {
- if (removeOld) {
- canvas.getObjects().forEach(g => g.__tt == 'ruler' ? canvas.remove(g) : '');
- }
- let measure = 'px';
- const width = canvas.getWidth() / canvas.getZoom();
- const height = canvas.getHeight() / canvas.getZoom();
- let minDistance = (width * .1);
- if (minDistance < 20) minDistance = 20;
- const ruler = [];
- let correctDistance = correctDistance = width / 10;
- let zoom = canvas.getZoom();
- let font_size = 12 / zoom;
- let cwidth = canvas.getWidth() / canvas.getZoom();
- let cheight = canvas.getHeight() / canvas.getZoom();
- let bg_color = "rgba(231, 231, 231, 1)",
- measure_bg_size = 20/zoom
- //подложка линейки горизональной
- var measure_bg_w = new fabric.Rect({
- left: measure_bg_size, //делаем отступ что бы линейка не налаживалась
- top: cheight-measure_bg_size,
- fill: bg_color,
- width: cwidth,
- height: measure_bg_size
- });
- //подложка линейки вертикальной
- var measure_bg_h = new fabric.Rect({
- left: 0,
- top: 0,
- width: measure_bg_size,
- fill: bg_color,
- height: cheight
- });
- ruler.push(measure_bg_w);
- ruler.push(measure_bg_h);
- if(false){
- let measure_text = new fabric.Text(measure, {
- fill: color,
- left: 30 / zoom,
- top: 30 / zoom,
- fontSize: font_size,
- textBackgroundColor: bg_color
- });
- ruler.push(measure_text);
- }
- let prev_text_step = 0,
- prev_line = 0;
- for (let i = 0; i < height / correctDistance; i++) {
- const lineOpts = {
- fill: color,
- stroke: color,
- strokeWidth: 1 / zoom
- };
- let leftText = false;
- let txt = (measure == 'px') ? (`${Math.round((i * correctDistance) / zoomOrigin)}`) : `${i}`;
- if (txt != "0"){
- let text_step = (i * correctDistance);
- if (text_step - prev_line > (10 / zoom)) {
- if (text_step <= height - 1) {
- ruler.push(new fabric.Line([
- 0, text_step,
- 13 / zoom, text_step
- ], lineOpts));
- prev_line = text_step;
- }
- }
- if (text_step - prev_text_step < (100 / zoom)) {
- } else {
- leftText = new fabric.Text(txt, {
- fill: color,
- left: 20 / zoom,
- top: (text_step) - (6 / zoom),
- fontSize: font_size,
- textBackgroundColor: bg_color
- });
- prev_text_step = text_step;
- }
- }
- const miniStep = (((i + 1) * correctDistance) - (i * correctDistance)) / 10;
- if (miniStep >= 2) {
- if (i * correctDistance <= height - 1) {
- const coords = new Array(9).fill(0).reduce((result, item, index) => {
- const left = (i * correctDistance) + ((index + 1) * miniStep);
- if (left + 1 <= height) {
- result.push(new fabric.Line([
- 0, left,
- (index == 4 ? 8 : 5) / zoom, left,
- ], lineOpts));
- }
- return result;
- }, []);
- ruler.push(...coords);
- }
- }
- if (leftText) {
- ruler.push(leftText);
- }
- }
- prev_text_step = 0;
- prev_line = 0;
- console.log("correctDistance", correctDistance, "full width", width);
- let center = width/2;
- for(let i=0; i<(width/correctDistance)/2; i++){
- let step_right = center+(i * correctDistance),
- step_left = center-(i * correctDistance);
- // ruler.push(new fabric.Line([
- // text_step,
- // cheight-(measure_bg_size-2),
- // text_step,
- // (cheight)
- // ], lineOpts));
- console.log("draw segments", step_left, step_right );
- }
- //слева на право, внизу.
- for(let i = 0; i < width / correctDistance; i++){
- const lineOpts = {
- fill: color, stroke: color,
- strokeWidth: 1 / zoom
- };
- let txt = (measure == 'px') ? (`${Math.round((i * correctDistance) / zoomOrigin)}`) : `${i}`;
- let topText = false;
- if (txt != "0") {
- let text_step = (i * correctDistance);
- //рисуем большие полоски
- if (text_step - prev_line > (10 / zoom)) {
- if (text_step <= width - 1) {
- ruler.push(new fabric.Line([
- text_step,
- cheight-(measure_bg_size-2),
- text_step,
- (cheight)
- ], lineOpts));
- }
- prev_line = text_step;
- }
- //height
- //рисуем текста
- if (text_step - prev_text_step > (100 / zoom)) {
- // topText = new fabric.Text(txt, {
- // fill: color,
- // left: (i * correctDistance),
- // top: cheight-(16 / zoom),
- // fontSize: font_size,
- // textBackgroundColor: bg_color
- // });
- // topText.set({
- // left: topText.left - (i == 0 ? 0 : (topText.width / 2))
- // });
- prev_text_step = text_step;
- }
- }
- //100
- //рисум маленькие полоски
- const miniStep = (((i + 1) * correctDistance) - (i * correctDistance)) / (10);
- if (miniStep >= 2) {
- if (i * correctDistance <= width - 1) {
- let prev_min = (i * correctDistance) + ((0 + 1) * miniStep);
- const coords = new Array(9).fill(0).reduce((result, item, index) => {
- const left = (i * correctDistance) + ((index + 1) * miniStep);
- let top = 100;
- let line_v_offset = measure_bg_size-2;
- if (left + 1 <= width) {
- let line_color = "red";
- if(index == 4){
- line_color = "blue";
- }
- //line
- lineOpts.fill = line_color;
- lineOpts.stroke = line_color;
- lineOpts.color = line_color
- result.push(new fabric.Line([
- left,
- cheight-line_v_offset, //top
- left,
- (index == 4 ? (cheight-(measure_bg_size/2)) : (cheight-(measure_bg_size/1.5))) / zoom //height
- ], lineOpts));
- }
- prev_min = left;
- return result;
- }, []);
- ruler.push(...coords);
- }
- }
- if (topText) {
- ruler.push(topText);
- }
- }
- return new fabric.Group(ruler, {
- measure: measure,
- // objectCaching: true,
- hoverCursor: 'default',
- __tt: 'ruler',
- selectable: false,
- evented: false,
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement