简体   繁体   English

当我将光标移出画布时,为什么我的图形不可见?

[英]Why does my drawing becomes invisible when I take my cursor out of the canvas?

I am using fabric.js to build a custom T-Shirt design panel. 我正在使用fabric.js构建自定义的T恤设计面板。 I have upload image, enter texts etc options which work just fine. 我上传了图片,输入了文字等选项,效果很好。 But when I added the drawing tool's JavaScript code from fabric.js library and faced a few problem: 但是,当我从fabric.js库添加绘图工具的JavaScript代码时,遇到了一些问题:

  1. After drawing anything, if I take my cursor out of canvas, the drawing is not visible. 绘制完所有内容后,如果我将光标移出画布,则看不到该绘图。 If I click back in canvas, it's visible. 如果我在画布上单击回来,它是可见的。

  2. If I upload image, it's visible in canvas but disappears when I click on canvas. 如果我上传图像,则它在画布中可见,但是当我单击画布时消失。

All the issues are gone if I comment out this part of code but as it's mandatory for drawing anything in the canvas! 如果我注释掉这部分代码,所有问题都消失了,但是在画布上绘制任何内容都是必不可少的!

var canvas = this.__canvas = new fabric.Canvas('tcanvas', {
isDrawingMode: false
});

I am adding images and the JS code for the drawing tool. 我正在为绘图工具添加图像和JS代码。

            (function() {
            var $ = function(id){return document.getElementById(id)};

            var canvas = this.__canvas = new fabric.Canvas('tcanvas', {
                isDrawingMode: true
            });

            fabric.Object.prototype.transparentCorners = false;

            var drawingModeEl = $('drawing-mode'),
                drawingOptionsEl = $('drawing-mode-options'),
                drawingColorEl = $('drawing-color'),
                drawingShadowColorEl = $('drawing-shadow-color'),
                drawingLineWidthEl = $('drawing-line-width'),
                drawingShadowWidth = $('drawing-shadow-width'),
                drawingShadowOffset = $('drawing-shadow-offset'),
                clearEl = $('clear-canvas');

            clearEl.onclick = function() { canvas.clear() };

            drawingModeEl.onclick = function() {
                canvas.isDrawingMode = !canvas.isDrawingMode;
                if (canvas.isDrawingMode) {
                drawingModeEl.innerHTML = 'Cancel drawing mode';
                drawingOptionsEl.style.display = '';
                }
                else {
                drawingModeEl.innerHTML = 'Enter drawing mode';
                drawingOptionsEl.style.display = 'none';
                }
            };

            if (fabric.PatternBrush) {
                var vLinePatternBrush = new fabric.PatternBrush(canvas);
                vLinePatternBrush.getPatternSrc = function() {

                var patternCanvas = fabric.document.createElement('canvas');
                patternCanvas.width = patternCanvas.height = 10;
                var ctx = patternCanvas.getContext('2d');

                ctx.strokeStyle = this.color;
                ctx.lineWidth = 5;
                ctx.beginPath();
                ctx.moveTo(0, 5);
                ctx.lineTo(10, 5);
                ctx.closePath();
                ctx.stroke();

                return patternCanvas;
                };

                var hLinePatternBrush = new fabric.PatternBrush(canvas);
                hLinePatternBrush.getPatternSrc = function() {

                var patternCanvas = fabric.document.createElement('canvas');
                patternCanvas.width = patternCanvas.height = 10;
                var ctx = patternCanvas.getContext('2d');

                ctx.strokeStyle = this.color;
                ctx.lineWidth = 5;
                ctx.beginPath();
                ctx.moveTo(5, 0);
                ctx.lineTo(5, 10);
                ctx.closePath();
                ctx.stroke();

                return patternCanvas;
                };

                var squarePatternBrush = new fabric.PatternBrush(canvas);
                squarePatternBrush.getPatternSrc = function() {

                var squareWidth = 10, squareDistance = 2;

                var patternCanvas = fabric.document.createElement('canvas');
                patternCanvas.width = patternCanvas.height = squareWidth + squareDistance;
                var ctx = patternCanvas.getContext('2d');

                ctx.fillStyle = this.color;
                ctx.fillRect(0, 0, squareWidth, squareWidth);

                return patternCanvas;
                };

                var diamondPatternBrush = new fabric.PatternBrush(canvas);
                diamondPatternBrush.getPatternSrc = function() {

                var squareWidth = 10, squareDistance = 5;
                var patternCanvas = fabric.document.createElement('canvas');
                var rect = new fabric.Rect({
                    width: squareWidth,
                    height: squareWidth,
                    angle: 45,
                    fill: this.color
                });

                var canvasWidth = rect.getBoundingRect().width;

                patternCanvas.width = patternCanvas.height = canvasWidth + squareDistance;
                rect.set({ left: canvasWidth / 2, top: canvasWidth / 2 });

                var ctx = patternCanvas.getContext('2d');
                rect.render(ctx);

                return patternCanvas;
                };

                var img = new Image();
                img.src = '../assets/honey_im_subtle.png';

                var texturePatternBrush = new fabric.PatternBrush(canvas);
                texturePatternBrush.source = img;
            }

            $('drawing-mode-selector').onchange = function() {

                if (this.value === 'hline') {
                canvas.freeDrawingBrush = vLinePatternBrush;
                }
                else if (this.value === 'vline') {
                canvas.freeDrawingBrush = hLinePatternBrush;
                }
                else if (this.value === 'square') {
                canvas.freeDrawingBrush = squarePatternBrush;
                }
                else if (this.value === 'diamond') {
                canvas.freeDrawingBrush = diamondPatternBrush;
                }
                else if (this.value === 'texture') {
                canvas.freeDrawingBrush = texturePatternBrush;
                }
                else {
                canvas.freeDrawingBrush = new fabric[this.value + 'Brush'](canvas);
                }

                if (canvas.freeDrawingBrush) {
                canvas.freeDrawingBrush.color = drawingColorEl.value;
                canvas.freeDrawingBrush.width = parseInt(drawingLineWidthEl.value, 10) || 1;
                canvas.freeDrawingBrush.shadow = new fabric.Shadow({
                    blur: parseInt(drawingShadowWidth.value, 10) || 0,
                    offsetX: 0,
                    offsetY: 0,
                    affectStroke: true,
                    color: drawingShadowColorEl.value,
                });
                }
            };

            drawingColorEl.onchange = function() {
                canvas.freeDrawingBrush.color = this.value;
            };
            drawingShadowColorEl.onchange = function() {
                canvas.freeDrawingBrush.shadow.color = this.value;
            };
            drawingLineWidthEl.onchange = function() {
                canvas.freeDrawingBrush.width = parseInt(this.value, 10) || 1;
                this.previousSibling.innerHTML = this.value;
            };
            drawingShadowWidth.onchange = function() {
                canvas.freeDrawingBrush.shadow.blur = parseInt(this.value, 10) || 0;
                this.previousSibling.innerHTML = this.value;
            };
            drawingShadowOffset.onchange = function() {
                canvas.freeDrawingBrush.shadow.offsetX = parseInt(this.value, 10) || 0;
                canvas.freeDrawingBrush.shadow.offsetY = parseInt(this.value, 10) || 0;
                this.previousSibling.innerHTML = this.value;
            };

            if (canvas.freeDrawingBrush) {
                canvas.freeDrawingBrush.color = drawingColorEl.value;
                canvas.freeDrawingBrush.width = parseInt(drawingLineWidthEl.value, 10) || 1;
                canvas.freeDrawingBrush.shadow = new fabric.Shadow({
                blur: parseInt(drawingShadowWidth.value, 10) || 0,
                offsetX: 0,
                offsetY: 0,
                affectStroke: true,
                color: drawingShadowColorEl.value,
                });
            }
            })();

图片参考当我在绘制后将光标放在画布上时

图片参考当我在绘制后从画布上删除光标时

There is a known bug with drawing mode in that if the freeDrawingBrush.onMouseUp() event is not fired then the points in the drawing just made is not converted to a path. 绘图模式存在一个已知的错误 ,即如果未触发freeDrawingBrush.onMouseUp()事件,则刚才绘制的点不会转换为路径。

Whenever you switch drawing mode from your drawing controls, make sure you call 每当您从绘图控件切换绘图模式时,请确保调用

canvas.freeDrawingBrush.onMouseUp();

Here is a snippet that lets you toogle the call the that method. 这是一个片段,可让您切换该方法的调用。 With it OFF the line is erased. 关闭时,线路被擦除。 With the toggle ON the line is preserved. 拨动打开时,线路将保留。

 var canvas = new fabric.Canvas("c"); var toggleVal = 1; $('#toggle').on('click', function(e){ toggleVal = (toggleVal === 1 ? 0 : 1); $(this).html((toggleVal === 1 ? 'On' : 'Off')); $(this).val(toggleVal) clearTimeout(timer); reset(); }) var timer; function reset() { canvas.clear(); canvas.isDrawingMode = true; canvas.freeDrawingBrush.color = "green"; canvas.freeDrawingBrush.width = 4; timer = setTimeout(stop_drawing, 3000); } function stop_drawing() { canvas.isDrawingMode = false; if (toggleVal == 1){ canvas.freeDrawingBrush.onMouseUp(); } } reset(); 
 div { background-color: silver; width: 600px; height: 300px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.1/fabric.min.js"></script> <p>Click the canvas and draw a line. Shortly the timeout fires and drawing stops. Now click the canvas again.</p> <p>The toggle button toggles mouse-up catching - off makes your line dissapear on next canvas click,<br />whilst on works more intuitively <button id='toggle' >On</button></p> <div> <canvas id="c" width="600" height="300"></canvas> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的文字会短暂显示,然后突然变得不可见? - Why does my text briefly show up and then suddenly becomes invisible? 为什么我的线无法绘制到画布上? - Why is my line not drawing to the canvas? 为什么打开画布时我的固定菜单将固定div推开了? - Why does my off-canvas menu push my fixed div out of frame when opened? HTML-放大/缩小时,我的内容变得混乱 - HTML - My content becomes messed up when I zoom in/out 为什么我的绘图没有显示在画布上? - Why isn't my drawing showing on canvas? 为什么我的绘图 canvas 在页面内发生偏移? - Why is my drawing canvas offset within the page? 为什么当我调整 select 的大小时,第一个元素被选中? - Why is it that when I resize my select the first element becomes selected? 为什么我只能在我的 Canvas 的中心画画而不能真正跟随我的 cursor - Why i can only draw in the center of my Canvas and draw not really follow my cursor 为什么我的jquery在滚动动画时需要这么长时间? - Why does my jquery take so long when animating on scroll? 我正在尝试使用 javascript 创建一个绘图应用程序,但它应该发生的 canvas 元素没有响应。 我的代码有什么问题? - I'm trying to create a drawing app with javascript, but the canvas element it's supposed to take place in is unresponsive. What's wrong with my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM