Javascript ateList({getClosestline:getClosestline,draw:drawLines,points:points}), 就绪:错误, },{ 线型:{lineWidth:2,strokeStyle:“蓝色”,fillStyle:“蓝色

Javascript ateList({getClosestline:getClosestline,draw:drawLines,points:points}), 就绪:错误, },{ 线型:{lineWidth:2,strokeStyle:“蓝色”,fillStyle:“蓝色,javascript,jquery,d3.js,canvas,mousemove,Javascript,Jquery,D3.js,Canvas,Mousemove,ateList({getClosestline:getClosestline,draw:drawLines,points:points}), 就绪:错误, },{ 线型:{lineWidth:2,strokeStyle:“蓝色”,fillStyle:“蓝色”}, 点样式:{线宽:1,笔划样式:“蓝色”,填充样式:“蓝色”}, 字体:{fillStyle:“蓝色”,字体:“18px arial”,文本对齐:“左”}, 背景:ctx2, 画布:画布2, 点:(点=创建列表({getClosestPo

ateList({getClosestline:getClosestline,draw:drawLines,points:points}), 就绪:错误, },{ 线型:{lineWidth:2,strokeStyle:“蓝色”,fillStyle:“蓝色”}, 点样式:{线宽:1,笔划样式:“蓝色”,填充样式:“蓝色”}, 字体:{fillStyle:“蓝色”,字体:“18px arial”,文本对齐:“左”}, 背景:ctx2, 画布:画布2, 点:(点=创建列表({getClosestPoint:getClosestPoint,draw:drawPoints})), 线条:createList({getClosestline:getClosestline,draw:drawLines,points:points}), 就绪:错误, },{ 线型:{lineWidth:2,strokeStyle:“gold”,fillStyle:“gold”}, 点样式:{lineWidth:1,strokeStyle:“gold”,fillStyle:“gold”}, 字体:{fillStyle:“gold”,字体:“18px arial”,文本对齐:“left”}, 背景:ctx3, 画布:画布3, 点:(点=创建列表({getClosestPoint:getClosestPoint,draw:drawPoints})), 线条:createList({getClosestline:getClosestline,draw:drawLines,points:points}), 就绪:错误, },{ 线型:{lineWidth:2,strokeStyle:“aqua”,fillStyle:“aqua”}, 点样式:{lineWidth:1,strokeStyle:“aqua”,fillStyle:“aqua”}, 字体:{fillStyle:“aqua”,字体:“18px arial”,文本对齐:“left”}, 上下文:ctx4, 画布:画布4, 点:(点=创建列表({getClosestPoint:getClosestPoint,draw:drawPoints})), 线条:createList({getClosestline:getClosestline,draw:drawLines,points:points}), 就绪:错误, } ]; var-currentLayer=0; 常量高亮样式={ 线宽:3, strokeStyle:“红色”, } 常量字体={ 字体:“18px arial”, 填充样式:“黑色”, textAlign:“居中”, } //主更新功能 功能更新(计时器){ 如果(鼠标按钮){ if(鼠标x<50和鼠标y<28*5){ mouse.drag=mouse.button=mouse.dragStart=false; currentLayer=(鼠标y/28)| 0; eachOf(层,层=>layer.ready=false); } } 常数层=层[currentLayer]; const ctx=layer.context; const canvas=layer.canvas; 常数线=层线; 常量点=图层点; const lineStyle=layer.lineStyle; const pointStyle=layer.pointStyle; 光标=“十字线”; toolTip=helpCount<2?“单击并拖动以创建一行”:“”; 全局时间=计时器; setTransform(1,0,0,1,0,0);//重置转换 ctx.globalAlpha=1;//重置alpha 如果(w!==内部宽度| | h!==内部高度){ cw=(w=canvas1.width=innerWidth)/2; ch=(h=canvas1.height=innerHeight)/2; canvas2.height=canvas3.height=canvas4.height=h; canvas2.width=canvas3.width=canvas4.width=w; eachOf(层,层=>layer.ready=false); }否则{ ctx.clearRect(0,0,w,h); } 如果(鼠标拖动===false){ if(鼠标x<50和鼠标y<28*5){ 游标=“指针”; toolTip=“单击以选择图层”; closestPoint=closestLine=未定义; }否则{ closestLine=未定义; closestPoint=points.getClosest(鼠标,思维者); if(closestPoint==未定义){ closestLine=lines.getClosest(鼠标,思维者); } if(闭合点| |闭合线){ toolTip=“单击并拖动以移动“+(闭合点?):“线”); 光标=“移动”; } } } if(mouse.dragStart){ if(闭合点){ dragOffsetX=closestPoint.x-mouse.x; dragOffsetY=closestPoint.y-mouse.y; pointDrag=true; }else if(闭合线){ dragOffsetX=points.items[closestLine.p1].x-mouse.x; dragOffsetY=points.items[closestLine.p1].y-mouse.y; pointDrag=false; }否则{ 添加(点2(mouse.x,mouse.y)); closestPoint=points.add(Point2(mouse.x,mouse.y)); closestLine=lines.add(行(points.items.length-2,points.items.length-1)); dragOffsetX=0; dragOffsetY=0; pointDrag=true; helpCount+=1; } mouse.dragStart=false; }else if(鼠标拖动){ 游标='无'; 如果(点拖动){ closestPoint.x=mouse.x+dragOffsetX; closestPoint.y=mouse.y+dragOffsetY; }否则{ const dx=mouse.x-mouse.dragStartX; const dy=mouse.y-mouse.dragStartY; mouse.dragStartX=mouse.x; mouse.dragStartY=mouse.y; points.items[closestLine.p1].x+=dx; points.items[closestLine.p1].y+=dy; points.items[closestLine.p2].x+=dx; points.items[closestLine.p2].y+=dy; } }否则{ } //画出所有的点和线 设置样式(线型,ctx); ctx.beginPath(); 线图(ctx); ctx.stroke(); 设置样式(点样式,ctx); ctx.beginPath(); 点。绘制(ctx); ctx.stroke(); //画突出显示的点或线 设置样式(highlightStyle,ctx); ctx.beginPath(); if(closestLine){绘图线(closestLine,ctx,points)} if(closestPoint){drawPoint(closestPoint,ctx)} ctx.stroke(); 每个(层,(层,i)=>{ 如果(!layer.ready){ const ctx=layer.context; ctx.globalAlpha=0.75; ctx.clearRect(0,0,w,h); 设置样式(图层线样式,ctx); ctx.beginPath(); 层线图(ctx); ctx.stroke(); 设置样式(图层点样式,ctx); ctx.beginPath(); 图层点绘制(ctx); ctx.stroke(); setStyle(图层字体,ctx); ctx.fillText(“层”+(i+1),10,i*28+28); layer.ready=true; ctx.globalAlpha=1; } }); setStyle(图层字体,ctx); ctx.fillText(“图层打开”,10,currentLayer*28+28); 如果(帮助计数<2){ 设置样式(字体,ctx); ctx.fillText(工具提示,cw,30); } canvas4.style.cursor=光标; 如果(帮助计数<5){ canvas4.title=工具提示; }否则{ canvas4.title=“单击图层进行选择”; } requestAnimationFrame(更新); } requestAnimationFrame(更新)
canvas{
位置:绝对位置;
顶部:0px;
左:0px;
}
<
  // canvas data layers
  ["marks", "foreground", "brushed", "highlight", "clickable_colors"].forEach(function(layer, i) {
    canvas[layer] = selection
      .append("canvas")
      .attr({
      id: layer, //added an id for easier selecting for mouse event 
      class: layer,
      style: "z-index: " + i +10000000
      })[0][0];
    ctx[layer] = canvas[layer].getContext("2d");
  });
var my_clickable_canvas = document.getElementById('clickable_colors');
var context = my_clickable_canvas.getContext('2d');

context.fillStyle = "rgb(255,0,0)";
context.fillRect(0, 0, 50, 50);
context.fillStyle = "rgb(0,0,255)";
context.fillRect(55, 0, 50, 50);

$("#clickable_colors").mousemove(function(e) {
    debugger;
    var pos = findPos(this);
    var x = e.pageX - pos.x;
    console.log(x)
    var y = e.pageY - pos.y;
    var coord = "x=" + x + ", y=" + y;
    var c = this.getContext('2d');
    var p = c.getImageData(x, y, 1, 1).data; 
    var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
    $('#examplecanvas').html(coord + "<br>" + hex);
    console.log("hi")
});