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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| <!DOCTYPE html> <html>
<head> <title>画布操作(toDataUrl)</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="图形系统开发实战:基础篇 示例"> <meta name="author" content="hjq"> <meta name="keywords" content="canvas,ladder,javascript"> <style> img { margin-left: 10px } </style> </head>
<body style="overflow: hidden;"> <div style="margin:0 auto; width:900px;"> <div style="margin:0 auto; width:600px;"> <canvas id="canvas" width="800" height="600" style="border:solid 10px goldenrod;"></canvas> </div> <div id="toolbar" style="text-align:center;"></div> </div> </body> <script> let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d');
let image = new Image(); image.onload = function () { canvas.width = image.width; canvas.height = image.height; canvas.parentElement.style.width = (canvas.width + 20)+ "px"; ctx.drawImage(image, 0, 0); filterImage(sepia, "怀旧"); filterImage(grayscaleFilter, "灰度"); filterImage(brighten, "亮度调节"); filterImage(noise, "噪点调节"); filterImage(invert, "反色"); drawText("原 图", ctx); } image.src = "/examples/canvas-qa/canvas_1b/images/girl.png";
function filterImage(filter, text) { let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); filter(imageData)
let tempCanvas = document.createElement("canvas"); tempCanvas.width = imageData.width; tempCanvas.height = imageData.height; let context = tempCanvas.getContext("2d"); context.putImageData(imageData, 0, 0);
drawText(text, context);
let base64url = tempCanvas.toDataURL();
let img = document.createElement("img"); img.width = 160; img.src = base64url; document.getElementById("toolbar").appendChild(img);
tempCanvas.width = 0; tempCanvas.height = 0; tempCanvas = null; }
function drawText(text, context) { context.save(); context.font = "24px 黑体" context.textAlign = "center"; context.strokeStyle="white"; context.lineWidth = 6; context.strokeText(text, context.canvas.width/2, 30); context.fillText(text, context.canvas.width/2, 30); context.restore(); }
function grayscaleFilter(imageData) { let data = imageData.data, brightness; for (let i = 0; i < data.length; i += 4) { brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2]; data[i] = brightness; data[i + 1] = brightness; data[i + 2] = brightness; } }
function brighten(imageData, options = {}) { let data = imageData.data, brightness = (options.brightness || 0.2) * 255; for (let i = 0; i < data.length; i += 4) { data[i] += brightness; data[i + 1] += brightness; data[i + 2] += brightness; } };
function invert(imageData) { let data = imageData.data; for (let i = 0; i < data.length; i += 4) { data[i] = 255 - data[i]; data[i + 1] = 255 - data[i + 1]; data[i + 2] = 255 - data[i + 2]; } };
function sepia(imageData) { let data = imageData.data, i, r, g, b; for (i = 0; i < data.length; i += 4) { r = data[i + 0]; g = data[i + 1]; b = data[i + 2]; data[i + 0] = Math.min(255, r * 0.393 + g * 0.769 + b * 0.189); data[i + 1] = Math.min(255, r * 0.349 + g * 0.686 + b * 0.168); data[i + 2] = Math.min(255, r * 0.272 + g * 0.534 + b * 0.131); } }
function noise(imageData, options = {}) { let noise = options.noise || 0.4; let amount = noise * 255, data = imageData.data, half = amount / 2; for (let i = 0; i < data.length; i += 4) { data[i + 0] += half - 2 * half * Math.random(); data[i + 1] += half - 2 * half * Math.random(); data[i + 2] += half - 2 * half * Math.random(); } } </script>
</html>
|