example4.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Jimp browser example 4</title>
  5. </head>
  6. <body>
  7. <h1> Demonstrates how to write a text over an image </h1>
  8. <script src="../lib/jimp.js"></script>
  9. <img id="pic" style="float: left; margin: 0px 20px 5px 0px;">
  10. <script>
  11. function writePic() {
  12. Jimp.read("lenna.png")
  13. .then(function (lenna) {
  14. var fntName = "FONT_SANS_"+fntSize.value+"_"+txtColor.value;
  15. var x = parseInt(txtX.value);
  16. var y = parseInt(txtY.value);
  17. var w = parseInt(txtW.value);
  18. console.log()
  19. Jimp.loadFont(Jimp[fntName]).then(function (font) {
  20. lenna.print(font, x, y, txtField.value, w)
  21. .getBase64(Jimp.AUTO, function (err, src) {
  22. if (err) {
  23. alert(err.message);
  24. throw err;
  25. }
  26. pic.setAttribute("src", src);
  27. });
  28. }).catch(function (err) { throw err });
  29. })
  30. .catch(function (err) {
  31. alert("Image load fail.\n"+err.message)
  32. throw err;
  33. });
  34. return false;
  35. }
  36. writePic();
  37. </script>
  38. <textarea rows="4" cols="80" id="txtField">Hi! I'm Lenna.</textarea>
  39. <br> <label>Size: <select id="fntSize">
  40. <option>8</option>
  41. <option>16</option>
  42. <option>32</option>
  43. <option>64</option>
  44. <option>128</option>
  45. </select></label>
  46. <br> <label>Color: <select id="txtColor">
  47. <option>BLACK</option>
  48. <option>WHITE</option>
  49. </select></label>
  50. <br> <label>pos X: <input id="txtX" value="0" size="3">px</label>
  51. <br> <label>pos Y: <input id="txtY" value="0" size="3">px</label>
  52. <br> <label>max Width: <input id="txtW" value="400" size="3">px</label>
  53. <br> <input type="button" id="btWrite" onclick="writePic(); return false" value="Write">
  54. </body>
  55. </html>