简体   繁体   English

如何在复杂物体上绘画

[英]How to paint over a complex object

I do have a fairly complex figure painted on canvas. 我确实在画布上画了一个相当复杂的人物。 (~ 1000 polygons). (〜1000个多边形)。 The repainting time for all of them is about 1 sec (very slow). 它们所有的重涂时间约为1秒(非常慢)。 Now I need to let user move over that figure and display a vertical and horizontal lines (cross hairs) from under mouse position. 现在,我需要让用户移至该图上并从鼠标位置下方显示一条垂直线和一条水平线(十字线)。 What is the best technique to paint only those 2 lines without going over all polygons and repaint everything at every mouse move. 最好的技术是仅绘制这两条线,而无需遍历所有多边形并在每次鼠标移动时重新绘制所有内容。

Thx 谢谢

This answer is broken. 这个答案坏了。

You want to draw lines and move them without touching the underlying painting. 您想画线并移动它们而不会碰到基础绘画。 In the good old days, the method used was to paint with xor composition on top of the drawing, and draw the same pattern (here it would be lines) the same way at the same location to remove it from the screen, again without touching the real painting. 在过去的好日子里,使用的方法是在图形的顶部用异或组成进行绘制,并在相同的位置以相同的方式绘制相同的图案(此处为线条)以将其从屏幕上移除,而无需再次触摸真正的绘画。

I tried to use this method with the code below to test it out, but it doesn't seem to work. 我尝试将这种方法与下面的代码一起使用以对其进行测试,但它似乎不起作用。 Hopefully, someone with a better knowledge of canvas and js will come forth and fix things up. 希望有一个对canvas和js有更好了解的人会解决这个问题。

function onmousemove(e){
  // firt run
  var tcanvas = document.getElementById("testCanvas");
  var tcontext = tcanvas.getContext("2d");
  var pos = {x : e.clientX, y : e.clientY, 
             w : tcanvas.width, h : tcanvas.height };
  var comp = tcontext.globalCompositeOperation;
  var paintline = function (p){
      tcontext.save();

      tcontext.lineWidth = 1;
      tcontext.globalCompositeOperation = 'xor';
      tcontext.fillStyle = "#000000";

      tcontext.moveTo(0,p.y);
      tcontext.lineTo(p.w,p.y);
      tcontext.moveTo(p.x,0);
      tcontext.lineTo(p.x,p.h);
      tcontext.stroke();

      tcontext.restore();
  };

  tcontext.save();
  paintline(pos);
  tcontext.restore();

  var next = function(e){
      var comp = tcontext.globalCompositeOperation;
      paintline(pos);
      pos = {x : e.clientX, y : e.clientY, 
             w : tcanvas.width, h : tcanvas.height };
      paintline(pos);
  };
  document.onmousemove = next;
}


(function draw_stuff(){
    // setup canvas
    var tcanvas = document.getElementById("testCanvas");
    var tcontext = tcanvas.getContext("2d");

    // draw square
    tcontext.fillStyle = "#FF3366";
    tcontext.fillRect(15,15,70,70);

    // set composite property
    tcontext.globalCompositeOperation = 'xor';

    // draw text
    tcontext.fillStyle="#0099FF";
    tcontext.font = "35px sans-serif";
    tcontext.fillText("test", 22, 25);
    // draw circle
    tcontext.fillStyle = "#0099FF";
    tcontext.beginPath();
    tcontext.arc(75,75,35,0,Math.PI*2,true);
    tcontext.fill();
    document.onmousemove = onmousemove; 
})();

Also, there are problem with compositing depending on the browser. 另外,取决于浏览器,合成也存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM