• js 自执行函数转d.ts之搬砖的代价.


    重复造轮子不可取,可是从这辆车的轮子换到另一不同品牌的车上, 也是一场重新塑造的过程.融合这块,往往也费时时费力,

    很多js 比如fabric.js 画图插件,功能很完善了. 用到ts 项目中,ts项目内无vue环境.

    只能通过 js 加一个 d.ts  声明使用 fabric 或者使用fabric的测试js .比如网上有有个测试 就是一个自行执行函数的测试例子js

    (function() {
      var $ = function(id){return document.getElementById(id)};
      var canvas = this.__canvas = new fabric.Canvas('c', {
        isDrawingMode: true
      });
      fabric.Object.prototype.transparentCorners = false;
    
      var drawingModeEl = $('drawing-mode'),
        drawingOptionsEl = $('drawing-mode-options'),
        drawingColorEl = $('drawing-color'),
        drawingShadowColorEl = $('drawing-shadow-color'),
        drawingLineWidthEl = $('drawing-line-width'),
        drawingShadowWidth = $('drawing-shadow-width'),
        drawingShadowOffset = $('drawing-shadow-offset'),
        clearEl = $('clear-canvas');
    
      clearEl.onclick = function() { canvas.clear() };
    
      drawingModeEl.onclick = function() {
        canvas.isDrawingMode = !canvas.isDrawingMode;
        if (canvas.isDrawingMode) {
          drawingModeEl.innerHTML = 'Cancel drawing mode';
          drawingOptionsEl.style.display = '';
        }
        else {
          drawingModeEl.innerHTML = 'Enter drawing mode';
          drawingOptionsEl.style.display = 'none';
        }
      };
    
      if (fabric.PatternBrush) {
        var vLinePatternBrush = new fabric.PatternBrush(canvas);
        vLinePatternBrush.getPatternSrc = function() {
    
          var patternCanvas = fabric.document.createElement('canvas');
          patternCanvas.width = patternCanvas.height = 10;
          var ctx = patternCanvas.getContext('2d');
    
          ctx.strokeStyle = this.color;
          ctx.lineWidth = 5;
          ctx.beginPath();
          ctx.moveTo(0, 5);
          ctx.lineTo(10, 5);
          ctx.closePath();
          ctx.stroke();
    
          return patternCanvas;
        };
    
        var hLinePatternBrush = new fabric.PatternBrush(canvas);
        hLinePatternBrush.getPatternSrc = function() {
    
          var patternCanvas = fabric.document.createElement('canvas');
          patternCanvas.width = patternCanvas.height = 10;
          var ctx = patternCanvas.getContext('2d');
    
          ctx.strokeStyle = this.color;
          ctx.lineWidth = 5;
          ctx.beginPath();
          ctx.moveTo(5, 0);
          ctx.lineTo(5, 10);
          ctx.closePath();
          ctx.stroke();
    
          return patternCanvas;
        };
    
        var squarePatternBrush = new fabric.PatternBrush(canvas);
        squarePatternBrush.getPatternSrc = function() {
    
          var squareWidth = 10, squareDistance = 2;
    
          var patternCanvas = fabric.document.createElement('canvas');
          patternCanvas.width = patternCanvas.height = squareWidth + squareDistance;
          var ctx = patternCanvas.getContext('2d');
    
          ctx.fillStyle = this.color;
          ctx.fillRect(0, 0, squareWidth, squareWidth);
    
          return patternCanvas;
        };
    
        var diamondPatternBrush = new fabric.PatternBrush(canvas);
        diamondPatternBrush.getPatternSrc = function() {
    
          var squareWidth = 10, squareDistance = 5;
          var patternCanvas = fabric.document.createElement('canvas');
          var rect = new fabric.Rect({
             squareWidth,
            height: squareWidth,
            angle: 45,
            fill: this.color
          });
    
          var canvasWidth = rect.getBoundingRect().width;
    
          patternCanvas.width = patternCanvas.height = canvasWidth + squareDistance;
          rect.set({ left: canvasWidth / 2, top: canvasWidth / 2 });
    
          var ctx = patternCanvas.getContext('2d');
          rect.render(ctx);
    
          return patternCanvas;
        };
    
        var img = new Image();
        img.src = '../assets/honey_im_subtle.png';
    
        var texturePatternBrush = new fabric.PatternBrush(canvas);
        texturePatternBrush.source = img;
      }
    
      $('drawing-mode-selector').onchange = function() {
    
        if (this.value === 'hline') {
          canvas.freeDrawingBrush = vLinePatternBrush;
        }
        else if (this.value === 'vline') {
          canvas.freeDrawingBrush = hLinePatternBrush;
        }
        else if (this.value === 'square') {
          canvas.freeDrawingBrush = squarePatternBrush;
        }
        else if (this.value === 'diamond') {
          canvas.freeDrawingBrush = diamondPatternBrush;
        }
        else if (this.value === 'texture') {
          canvas.freeDrawingBrush = texturePatternBrush;
        }
        else {
          canvas.freeDrawingBrush = new fabric[this.value + 'Brush'](canvas);
        }
    
        if (canvas.freeDrawingBrush) {
          var brush = canvas.freeDrawingBrush;
          brush.color = drawingColorEl.value;
          if (brush.getPatternSrc) {
            brush.source = brush.getPatternSrc.call(brush);
          }
          brush.width = parseInt(drawingLineWidthEl.value, 10) || 1;
          brush.shadow = new fabric.Shadow({
            blur: parseInt(drawingShadowWidth.value, 10) || 0,
            offsetX: 0,
            offsetY: 0,
            affectStroke: true,
            color: drawingShadowColorEl.value,
          });
        }
      };
    
      drawingColorEl.onchange = function() {
        var brush = canvas.freeDrawingBrush;
        brush.color = this.value;
        if (brush.getPatternSrc) {
          brush.source = brush.getPatternSrc.call(brush);
        }
      };
      drawingShadowColorEl.onchange = function() {
        canvas.freeDrawingBrush.shadow.color = this.value;
      };
      drawingLineWidthEl.onchange = function() {
        canvas.freeDrawingBrush.width = parseInt(this.value, 10) || 1;
        this.previousSibling.innerHTML = this.value;
      };
      drawingShadowWidth.onchange = function() {
        canvas.freeDrawingBrush.shadow.blur = parseInt(this.value, 10) || 0;
        this.previousSibling.innerHTML = this.value;
      };
      drawingShadowOffset.onchange = function() {
        canvas.freeDrawingBrush.shadow.offsetX = parseInt(this.value, 10) || 0;
        canvas.freeDrawingBrush.shadow.offsetY = parseInt(this.value, 10) || 0;
        this.previousSibling.innerHTML = this.value;
      };
    
      if (canvas.freeDrawingBrush) {
        canvas.freeDrawingBrush.color = drawingColorEl.value;
        canvas.freeDrawingBrush.source = canvas.freeDrawingBrush.getPatternSrc.call(this);
        canvas.freeDrawingBrush.width = parseInt(drawingLineWidthEl.value, 10) || 1;
        canvas.freeDrawingBrush.shadow = new fabric.Shadow({
          blur: parseInt(drawingShadowWidth.value, 10) || 0,
          offsetX: 0,
          offsetY: 0,
          affectStroke: true,
          color: drawingShadowColorEl.value,
        });
      }
    })();

    翻译成ts 文件 各种的错,放弃,直接使用呢,那得生成一个 此js的d.ts .webpack打包的时候 会识别这个js不会编译,但此测试js必须有d.ts

    这种用法估计网上很少, 整个过程就是跟编译器斗智斗勇啊..几近要放弃.后来瞎写一下,竟然成功了.

    问题出在fabric 无法传递到 这个测试js中, 

    /// <reference types="fabric"/> 引入到d.ts 编译器没给传递进去
    后来 修改一下测试js加上参数传递 

    还是提示fabric 未声明
    /// <reference types="fabric"/> 引入到d.ts  这个代码也无效啊..
    按照ztree 的方式也不行,ztree 用的jquery ,难道是因为jquery 是全局的导致吗.没细查.
    几近放弃,在index.ts 的引用页中我看
    import {fabric} from 'fabric'     fabric 灰白色,没有引用,直接给 markbar 的intMarkBar 一个未有的,无中生有的函数传递这个fabric 进去,竟然传递过去了.
    总结猜猜 匿名函数后括弧内的参数,可以通过下边的方式就能传递进去.
    import {fabric} from 'fabric'
    import {initMarkBar} from "./markbar";
    
    initMarkBar(fabric);

    因为测试js是自执行匿名函数,也不需要留出外边调用的方法等.开始想用匿名改为添加一个命名.后未去做,先在d.ts内 为空,结果报错找不到模块

    后在d.ts内任意加了个方法.结果不报错了.

     后记:尽管 test js 的方式也能用,毕竟还是个野路子.正规的方法就是npm fabric和types 文件后直接引用fabric ,使用fabric就可以了.test.js 只是证明对fabric不够熟悉,实际 测试了几个fabric ts实例 .也变得很简单.事物总是由模糊到熟练的过程.

    技术交流qq群:143280841
  • 相关阅读:
    使用TFS CI 又想保留服务运行状态的简单方法
    【知乎】二战中日本陆军的建制
    缓存你的BITMAP对象
    Android 多种方式正确的加载图像,有效避免oom
    GitHub进一步了解
    响应式编程,是明智的选择
    Android 主题动态切换框架:Prism
    Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
    ClassLoader工作机制
    Java——泛型(最易懂的方式讲解泛型)
  • 原文地址:https://www.cnblogs.com/zuochanzi/p/15427999.html
Copyright © 2020-2023  润新知