tempMap.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const defaultOptions = {
  2. 0: "#00FFFF",
  3. 0.2: "#0000FF",
  4. 0.25: "#00FFFF",
  5. 0.5: "#00FF00",
  6. 0.75: "#FFFF00",
  7. 0.8: "#FF0000",
  8. 0.9: "#FF0000",
  9. 1: "#ff0000",
  10. };
  11. export class TempMap {
  12. constructor(options) {
  13. this.options = Object.assign({}, defaultOptions, options);
  14. this.init();
  15. }
  16. init() {
  17. let colorStops = this.colorStops || defaultOptions;
  18. let canvas = document.createElement("canvas");
  19. canvas.width = 20;
  20. canvas.height = 256;
  21. let ctx = canvas.getContext("2d");
  22. let gradient = ctx.createLinearGradient(0, 0, 0, 255);
  23. for (let key in colorStops) {
  24. gradient.addColorStop(key, colorStops[key]);
  25. }
  26. ctx.fillStyle = gradient;
  27. ctx.fillRect(0, 0, 20, 256);
  28. this.imageData = ctx.getImageData(0, 0, 1, 255).data;
  29. this.canvas = canvas;
  30. }
  31. colorPicker(position) {
  32. return this.imageData.slice(position * 4, position * 4 + 3);
  33. }
  34. }
  35. if (typeof module !== "undefined") {
  36. module.exports = TempMap;
  37. }
  38. if (typeof define !== "undefined" && define.amd)
  39. define(function () {
  40. return TempMap;
  41. });
  42. if (typeof window !== "undefined") window.TempMap = TempMap;
  43. if (typeof exports !== "undefined") exports.TempMap = TempMap;