暂无描述

index.html 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!doctype html>
  2. <html>
  3. <meta charset="utf-8" />
  4. <script src="canvas2image.js"></script>
  5. <style>
  6. .doc {
  7. width: 604px;
  8. margin: 0 auto;
  9. }
  10. canvas {
  11. display: block;
  12. border: 2px solid #888;
  13. }
  14. </style>
  15. <body>
  16. <div class="doc">
  17. <canvas width="600" height="400" id="cvs"></canvas>
  18. <div>
  19. <p>
  20. <button id="save">save</button> or <button id="convert">convert to</button> as:
  21. <select id="sel">
  22. <option value="png">png</option>
  23. <option value="jpeg">jpeg</option>
  24. <option value="bmp">bmp</option>
  25. </select><br/>
  26. width : <input type="number" value="300" id="imgW" /><br/>
  27. height : <input type="number" value="200" id="imgH" /><br>
  28. fileName: <input type="text" placeholder="optional" id="imgFileName">
  29. </p>
  30. </div>
  31. <div id="imgs">
  32. </div>
  33. </div>
  34. <script>
  35. var canvas, ctx, bMouseIsDown = false, iLastX, iLastY,
  36. $save, $imgs,
  37. $convert, $imgW, $imgH,
  38. $sel;
  39. function init () {
  40. canvas = document.getElementById('cvs');
  41. ctx = canvas.getContext('2d');
  42. $save = document.getElementById('save');
  43. $convert = document.getElementById('convert');
  44. $sel = document.getElementById('sel');
  45. $imgs = document.getElementById('imgs');
  46. $imgW = document.getElementById('imgW');
  47. $imgH = document.getElementById('imgH');
  48. $imgFileName =document.getElementById('imgFileName');
  49. bind();
  50. draw();
  51. }
  52. function bind () {
  53. canvas.onmousedown = function(e) {
  54. bMouseIsDown = true;
  55. iLastX = e.clientX - canvas.offsetLeft + (window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft);
  56. iLastY = e.clientY - canvas.offsetTop + (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop);
  57. }
  58. canvas.onmouseup = function() {
  59. bMouseIsDown = false;
  60. iLastX = -1;
  61. iLastY = -1;
  62. }
  63. canvas.onmousemove = function(e) {
  64. if (bMouseIsDown) {
  65. var iX = e.clientX - canvas.offsetLeft + (window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft);
  66. var iY = e.clientY - canvas.offsetTop + (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop);
  67. ctx.moveTo(iLastX, iLastY);
  68. ctx.lineTo(iX, iY);
  69. ctx.stroke();
  70. iLastX = iX;
  71. iLastY = iY;
  72. }
  73. };
  74. $save.onclick = function (e) {
  75. var type = $sel.value,
  76. w = $imgW.value,
  77. h = $imgH.value;
  78. f = $imgFileName.value;
  79. Canvas2Image.saveAsImage(canvas, w, h, type,f);
  80. }
  81. $convert.onclick = function (e) {
  82. var type = $sel.value,
  83. w = $imgW.value,
  84. h = $imgH.value;
  85. $imgs.appendChild(Canvas2Image.convertToImage(canvas, w, h, type))
  86. }
  87. }
  88. function draw () {
  89. ctx.fillStyle = '#ffffff';
  90. ctx.fillRect(0, 0, 600, 400);
  91. ctx.fillStyle = 'red';
  92. ctx.fillRect(100, 100, 50, 50);
  93. }
  94. onload = init;
  95. </script>
  96. </body>
  97. </html>