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
| <!DOCTYPE html> <html>
<head> <meta charset="utf-8"> <title>裁剪图片</title> <style> canvas { border: 1px solid #CCC; border-radius: 4px; margin: 20px; } </style> </head>
<body> <canvas id="canvas" class="canvas" width="400" height="300"></canvas>
<script src="http://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script> <script> var canvas = $('#canvas').get(0); var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath(); context.fillStyle = '#CCC'; context.rect(80, 30, 150, 50); context.rect(110, 110, 50, 50); context.moveTo(210, 10); context.lineTo(50, 50); context.lineTo(150, 100); context.fill(); context.closePath(); </script>
<script> var bound = imageBoundingRect(canvas);
var subimage = context.getImageData(bound.x, bound.y, bound.w, bound.h); var newCanvas = $('<canvas id="temp-canvas" width="' + bound.w + '" height="' + bound.h + '"></canvas>').get(0); var newContext = newCanvas.getContext('2d'); newContext.putImageData(subimage, 0, 0); $(newCanvas).appendTo('body');
function imageBoundingRect(canvas, padding) { var imageData = context.getImageData(0, 0, canvas.width, canvas.height); var d = imageData.data; var w = imageData.width; var h = imageData.height;
var bound = { minX: 100000, minY: 100000, maxX: 0, maxY: 0 };
for (var x = 0; x < w; x++) { for (var y = 0; y < h; y++) { var r = d[y * w * 4 + x * 4 + 0]; var g = d[y * w * 4 + x * 4 + 1]; var b = d[y * w * 4 + x * 4 + 2]; var a = d[y * w * 4 + x * 4 + 3];
if (r !== 0 && g !== 0 && b !== 0 && a !== 0) { bound.minX = Math.min(bound.minX, x); bound.maxX = Math.max(bound.maxX, x); bound.minY = Math.min(bound.minY, y); bound.maxY = Math.max(bound.maxY, y); } } }
var p = padding || 5; bound.minX = Math.max(bound.minX - p, 0); bound.minY = Math.max(bound.minY - p, 0); bound.maxX = Math.min(bound.maxX + p, w); bound.maxY = Math.min(bound.maxY + p, h);
return { x: bound.minX, y: bound.minY, w: bound.maxX - bound.minX, h: bound.maxY - bound.minY }; } </script>
</body>
</html>
|