在canvas html5中删除上传图像的拖动属性(将区域设置为上传图像)

时间:2016-01-22 11:12:16

标签: javascript jquery html5 canvas fabricjs

我使用fabric js在画布上上传图片。现在它是一个可拖动的,但我想删除拖动属性并将区域设置为在画布上上传的图像。

HTML:

 <script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
        <canvas id="canvas" width="750" height="550"></canvas>
        <input type="file" id="file">

CSS:

 canvas{
                border: 1px solid black;
            }

JAVA SCRIPT:

 var canvas = new fabric.Canvas('canvas');
            document.getElementById('file').addEventListener("change", function (e) {
                var file = e.target.files[0];
                var reader = new FileReader();
                reader.onload = function (f) {
                    var data = f.target.result;
                    fabric.Image.fromURL(data, function (img) {
                        var oImg = img.set({left: 50, top: 100,width: 250, height: 200, angle: 00}).scale(0.9);
                        canvas.add(oImg).renderAll();
                        var a = canvas.setActiveObject(oImg);
                    });

                };
                reader.readAsDataURL(file);  
                var dataURL = canvas.toDataURL({
                    format: 'png',
                    opacity: 0.9
                });

            });

 
 
 var canvas = new fabric.Canvas('canvas');
            document.getElementById('file').addEventListener("change", function (e) {
                var file = e.target.files[0];
                var reader = new FileReader();
                reader.onload = function (f) {
                    var data = f.target.result;
                    fabric.Image.fromURL(data, function (img) {
                        var oImg = img.set({left: 50, top: 100,width:200,height:200, angle: 00}).scale(0.9);
                        canvas.add(oImg).renderAll();
                        var a = canvas.setActiveObject(oImg);
                    });

                };
                reader.readAsDataURL(file);  
                var dataURL = canvas.toDataURL({
                    format: 'png',
                    opacity: 0.9
                });
                console.log("Canvas Image " + dataURL);
            });
 canvas{
                border: 1px solid black;
            }
 <script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
        <canvas id="canvas" width="750" height="550"></canvas>
        <input type="file" id="file">

我只想删除已上传图片的拖动属性。

1 个答案:

答案 0 :(得分:0)

只需用

锁定它
.lockMovementX = true;
.lockMovementY = true;

var canvas = new fabric.Canvas('canvas');
            document.getElementById('file').addEventListener("change", function (e) {
                var file = e.target.files[0];
                var reader = new FileReader();
                reader.onload = function (f) {
                    var data = f.target.result;
                    fabric.Image.fromURL(data, function (img) {
                        var oImg = img.set({left: 50, top: 100,width:200,height:200, angle: 00}).scale(0.9);
                        oImg.lockMovementX = true;
                        oImg.lockMovementY = true;
                        canvas.add(oImg);
                        var a = canvas.setActiveObject(oImg);
                    });

                };
                reader.readAsDataURL(file);  
                var dataURL = canvas.toDataURL({
                    format: 'png',
                    opacity: 0.9
                });
                console.log("Canvas Image " + dataURL);
            });
canvas{
                border: 1px solid black;
            }
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
        <canvas id="canvas" width="750" height="550"></canvas>
        <input type="file" id="file">