vue-scratchable icon indicating copy to clipboard operation
vue-scratchable copied to clipboard

怎么设置涂刮层的图片大小

Open asdzsc opened this issue 2 years ago • 1 comments

涂刮层的图片大小和底层图片保持一致,这个要怎么设置?有什么api

asdzsc avatar May 25 '22 03:05 asdzsc

直接改源码,创建一个临时的canvas,用它画下resize过的pattern就行

const getResizedImagePattern = (
  ctx: CanvasRenderingContext2D,
  img: any,
  width: number,
  height: number,
  repeat = "repeat"
) => {
  const tempCanvas = document.createElement("canvas");
  const tCtx = tempCanvas.getContext("2d");

  tempCanvas.width = width;
  tempCanvas.height = height;
  tCtx?.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height);

  return ctx.createPattern(tempCanvas, repeat);
};

const setFillStyle = () => {
  const { type, value = "", src = "", repeat = "" } = props.hideOptions;
  if (!context.value) {
    return new Promise((resolve) => {
      resolve(null);
    });
  }
  context.value.globalCompositeOperation = "source-over";
  if (type === "color")
    return new Promise((resolve) => {
      resolve(() => {
        if (context.value) {
          context.value.fillStyle = value;
        }
      });
    });
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.onload = () => {
      if (context.value) {
        // const pattern = context.value.createPattern(img, repeat);
        const pattern = getResizedImagePattern(
          context.value,
          img,
          实际图片宽度(这里我直接通过props定义的),
          实际图片高度,
          repeat
        );
        if (pattern) {
          context.value.fillStyle = pattern;
        }
      }
      resolve(img);
    };
    img.onerror = (err) => reject(err);
    img.src = src;
  });
};

alphardex avatar May 31 '22 06:05 alphardex