--
--
應用
--
hoyo 自己的應用
需求為從主機載入一張圖之後,可以增加箭頭、文字、方框等基本的繪圖需求,編輯完成按儲存後要直接回存。
技術:HTML5 Canvas
使用工具:Fabric.js
參考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="../js/jquery-2.2.4.min.js"></script> <script src="../js/fabric.min.js"></script> </head> <body> <canvas id="c" style="border: 1px solid #999;"></canvas> <button onclick="addText()">增加文字</button> <button onclick="addBlock()">增加方框</button> <button onclick="addLine2()">增加箭頭</button> <button onclick="saveImage()">儲存</button> <script> var canvas = new fabric.Canvas('c'); var HideControls = { 'tl':true, 'tr':false, 'bl':true, 'br':true, 'ml':true, 'mt':true, 'mr':true, 'mb':true, 'mtr':true }; var image = new Image(); image.src = 'normal_3206.png'; image.onload = function() { // create a wrapper around native canvas element (with id="c") canvas.setHeight(image.width).setWidth(image.height); canvas.setBackgroundImage('normal_3206.png', canvas.renderAll.bind(canvas), { width: canvas.width, height: canvas.height, // Needed to position backgroundImage at 0/0 originX: 'left', originY: 'top' }); }; canvas.renderAll(); function addDeleteBtn(x, y){ $(".deleteBtn").remove(); var btnLeft = x-10; var btnTop = y-10; var deleteBtn = '<img src="../images/delete-icon.png" class="deleteBtn" style="position:absolute;top:'+btnTop+'px;left:'+btnLeft+'px;cursor:pointer;width:20px;height:20px;"/>'; $(".canvas-container").append(deleteBtn); } canvas.on('object:selected',function(e){ addDeleteBtn(e.target.oCoords.tr.x, e.target.oCoords.tr.y); }); canvas.on('mouse:down',function(e){ if(!canvas.getActiveObject()) { $(".deleteBtn").remove(); } }); canvas.on('object:modified',function(e){ addDeleteBtn(e.target.oCoords.tr.x, e.target.oCoords.tr.y); }); canvas.on('object:scaling',function(e){ $(".deleteBtn").remove(); }); canvas.on('object:moving',function(e){ $(".deleteBtn").remove(); }); canvas.on('object:rotating',function(e){ $(".deleteBtn").remove(); }); $(document).on('click',".deleteBtn",function(){ if(canvas.getActiveObject()) { canvas.remove(canvas.getActiveObject()); $(".deleteBtn").remove(); } }); // ---------------------------------------- function addBlock(){ var rect = new fabric.Rect({ stroke: 'rgb(255,0,0)', strokeWidth: 3, width: 100, height: 40, fill: false, left: 50, top: 50 }).setControlsVisibility(HideControls); canvas.add(rect); } function addText() { var t = new fabric.IText('Tap and Type', { fontFamily: 'arial black', left: 100, top: 100 }); var textWithBackground = new fabric.IText('輸入文字', { left: 50, top: 50, textBackgroundColor: 'rgb(255,255,255)' }).setControlsVisibility(HideControls); canvas.add(textWithBackground); } function addLine2(){ var path = new fabric.Path('M512 246.16H36.504l86.98-87.43v-27.895L0 254.943v2.115l123.474 124.107h.01V353.26l-86.98-87.42H512z', { stroke: 'rgb(255,255,255)', strokeWidth: 8, fill: 'rgb(255,0,0)' } ).scaleToWidth(100).setControlsVisibility(HideControls); canvas.add(path); } // 送出處理後圖像給 PHP function saveImage() { request = new XMLHttpRequest(); request.open('POST', 'c1.php', true); var $Json = {}; $Json['image'] = canvas.toDataURL(); $Json['id'] = 'aa111'; request.send( JSON.stringify($Json) ); } </script> </body> </html> |
--
1,680 total views, 2 views today