RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[canvas]

Martin Hope
dotagenius
Asked: 2022-07-10 22:45:32 +0000 UTC

如何在画布js中的浮动图像顶部显示文本?

  • 0

在这个问题的答案中,有一个画布中物体移动的例子。
如何在中心显示画布中的文本,以便画布的内容可以像以前一样移动,但文本保持静止?

我放置ctx.font = "48px serif"; ctx.fillText(text, canvas.width / 2, 100);在正方形的输出之后,但由于某种原因,文本附加到它们并移动。

const ctx = canvas.getContext("2d");
canvas.width = 500;
canvas.height = 500;
const rand = (m = 255, M = m + (m = 0)) => (Math.random() * (M - m) + m) | 0;


const objects = [];
for (let i = 0; i < 100; i++) {
  objects.push({x: rand(canvas.width), y: rand(canvas.height),w: rand(40),h: rand(40), col: `rgb(${rand()},${rand()},${rand()})`});
}

requestAnimationFrame(drawCanvas); 

const view = (() => {
  const matrix = [1, 0, 0, 1, 0, 0]; // current view transform
  var m = matrix;             // alias 
  var scale = 1;              // current scale
  var ctx;                    // reference to the 2D context
  const pos = { x: 0, y: 0 }; // current position of origin
  var dirty = true;
  const API = {
    set context(_ctx) { ctx = _ctx; dirty = true },
    apply() {
      if (dirty) { this.update() }
      ctx.setTransform(m[0], m[1], m[2], m[3], m[4], m[5])
    },
    get scale() { return scale },
    get position() { return pos },
    isDirty() { return dirty },
    update() {
      dirty = false;
      m[3] = m[0] = scale;
      m[2] = m[1] = 0;
      m[4] = pos.x;
      m[5] = pos.y;
    },
    pan(amount) {
      if (dirty) { this.update() }
       pos.x += amount.x;
       pos.y += amount.y;
       dirty = true;
    },
    scaleAt(at, amount) { // at in screen coords
      if (dirty) { this.update() }
      scale *= amount;
      pos.x = at.x - (at.x - pos.x) * amount;
      pos.y = at.y - (at.y - pos.y) * amount;
      dirty = true;
    },
  };
  return API;
})();
view.context = ctx;
function drawCanvas() {
    if (view.isDirty()) { 
        ctx.setTransform(1, 0, 0, 1, 0, 0); 
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        view.apply(); // set the 2D context transform to the view
        for (i = 0; i < objects.length; i++) {
            var obj = objects[i];
            ctx.fillStyle = obj.col;
            ctx.fillRect(obj.x, obj.y, obj.h, obj.h);
        }
        ctx.font = "48px serif";
        ctx.fillText("Hello World!", canvas.width / 2, 50);
    }
    requestAnimationFrame(drawCanvas);
}


canvas.addEventListener("mousemove", mouseEvent, {passive: true});
canvas.addEventListener("mousedown", mouseEvent, {passive: true});
canvas.addEventListener("mouseup", mouseEvent, {passive: true});
canvas.addEventListener("mouseout", mouseEvent, {passive: true});
canvas.addEventListener("wheel", mouseWheelEvent, {passive: false});
const mouse = {x: 0, y: 0, oldX: 0, oldY: 0, button: false};
function mouseEvent(event) {
    if (event.type === "mousedown") { mouse.button = true }
    if (event.type === "mouseup" || event.type === "mouseout") { mouse.button = false }
    mouse.oldX = mouse.x;
    mouse.oldY = mouse.y;
    mouse.x = event.offsetX;
    mouse.y = event.offsetY    
    if(mouse.button) { // pan
        view.pan({x: mouse.x - mouse.oldX, y: mouse.y - mouse.oldY});
    }
}
function mouseWheelEvent(event) {
    var x = event.offsetX;
    var y = event.offsetY;
    if (event.deltaY < 0) { view.scaleAt({x, y}, 1.1) }
    else { view.scaleAt({x, y}, 1 / 1.1) }
    event.preventDefault();
}
body {
  background: gainsboro;
  margin: 0;
}
canvas {
  background: white;
  box-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
}
<canvas id="canvas"></canvas>

画布中需要文本,因为我将画布的所有内容保存到图像中。

javascript canvas
  • 1 个回答
  • 23 Views
Martin Hope
user355286
Asked: 2020-07-14 10:15:31 +0000 UTC

如何动态修复奇数宽度线?

  • 1

如果加/减0.5px在这里有效

context1 = c1.getContext('2d')
context2 = c2.getContext('2d')

width = c1.width = c2.width = 160
height = c1.height = c2.height = 160

line(context1, width/2)
line(context2, width/2 + 0.5)

function line(context, x) {
  context.beginPath()
  context.moveTo(x, 0)
  context.lineTo(x, height)
  context.stroke()
}
body {
  margin: 0;
  height: 100vh;
}

canvas {
  display: inline-block;
  background: tomato;
}

canvas#c2 {
  background: lightgreen;
}
<canvas id=c1></canvas><canvas id=c2></canvas>

如果事先不知道尺寸canvas并且可以随时更改,该怎么办?

context = canvas.getContext('2d')
resize()
update()
onresize = resize

function resize() {
  width = canvas.width = innerWidth
  height = canvas.height = innerHeight
}

function update() {
  context.beginPath()
  context.moveTo(width/2, 0)
  context.lineTo(width/2, height)
  context.stroke()
  
  requestAnimationFrame(update)
}
body {
  margin: 0;
}

canvas {
  display: block;
}
<canvas id=canvas></canvas>

或者,例如,我怎么知道何时添加那些 0.5px 以便其上的线条和破折号在任何分辨率下看起来都正确?

context = canvas.getContext('2d')
onresize = resize
resize()

function resize() {
  width = canvas.width = innerWidth
  height = canvas.height = innerHeight
  cx = width / 2
  cy = height / 2
}

function coordinateSystem(gap) {
  context.lineWidth = 0.5

  drawLine(0, cy, width, cy)

  context.lineWidth = 1

  for (let i = 1; i < width / 2 / gap; i++) {
    let x = cx - i * gap
    hdash(x, cy)
    x = cx + i * gap
    hdash(x, cy)
  }
}

function drawLine(x1, y1, x2, y2) {
  context.beginPath()
  context.moveTo(x1, y1)
  context.lineTo(x2, y2)
  context.stroke()
}

function hdash(x, y) {
  drawLine(x, y - 7, x, y + 7)
}

function update() {
  context.clearRect(0, 0, width, height)
  coordinateSystem(20)
  requestAnimationFrame(update)
}

update()
body {
  margin: 0;
}

canvas {
  display: block;
}
<canvas id="canvas"></canvas>

我在这里阅读了答案,但说实话,没有任何帮助。

canvas
  • 1 个回答
  • 10 Views
Martin Hope
Lusy
Asked: 2020-04-12 00:47:26 +0000 UTC

如何在画布上添加画布清除功能

  • 2

帮助添加一个函数和一个按钮,在代码中绘制后清除画布。我尝试了很多方法,但它们只会卡住绘图功能。

    function init() {
      var tablet = document.getElementById("tablet");
      var ctx = tablet.getContext('2d');
      ctx.lineCap = 'round';
      var paint = false;
      tablet.addEventListener("mousedown", Down);
      tablet.addEventListener("mouseup", Up);
      tablet.addEventListener("mousemove", Move);

      function Down(e) {
        paint = true;
        ctx.beginPath();
        ctx.moveTo(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
      }

      function Up(e) {
        paint = false;
        ctx.closePath();
      }

      function Move(e) {
        if (!paint) return;
        ctx.lineTo(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
        ctx.stroke();
      }

      function setStyle() {
        ctx.strokeStyle = this.strokeStyle.value;
        ctx.lineWidth = this.lineWidth.value;
      }
      document.querySelector('form.canvas-attrs').addEventListener('change', setStyle);
    }

    init()
 <style>
    body {
      background: #ffffff url(https://drive.google.com/uc?export=view&id=1iPCcsPq2pPXDLrHbeq47qwnRSRwHitCZ);
      /* Цвет фона и путь к файлу */
      background-size: 1600px 700px;
      width: 1px;
      background-top: 10px;
      background-left: 100px;
      background-repeat: no-repeat;
      background-position: center top;
    }
#draggable { width: 32px; height: 32px; padding: 0.0em; }
    #draggable1 { width: 32px; height: 32px; padding: 0.0em; }
      #draggable2 { width: 32px; height: 32px; padding: 0.0em; }
 </style>

<script>
  $( function() {
    $( "#draggable" ).draggable();
    $( "#draggable1" ).draggable();
    $( "#draggable2" ).draggable();
</script>

<body>
<table>
  <tr>
<td><div id="draggable" class="ui-widget-content">
  <img src="https://drive.google.com/uc?export=download&id=1hSqUYfMvkYt3GAC1_aiKeL2QZeg5uP62" height="32" width="32"
</div></td>
 
<td> <div id="draggable1" class="ui-widget-content">
  <img src="https://drive.google.com/uc?export=download&id=1uuKROBRzA6xGtVc48xDf2KsSqR-1hpbe" height="32" width="32"
</div></td>

<td><div id="draggable2" class="ui-widget-content">
  <img src="https://drive.google.com/uc?export=download&id=1lG5A20O4avVJCsbfptRfZxPzcNBQ2b0w" height="32" width="32"
</div></td>
  </tr>
</table>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <form class="canvas-attrs">
      <p><b>Цвет маркера</b></p>
      <select name="strokeStyle">
        <option value="black">Черный</option>
        <option value="red" selected>Красный</option>
        <option value="green">Зеленый</option>
      </select>
      <p><b>Размер маркера</b></p>
      <select name="lineWidth">
        <option value="1">Маленькая</option>
        <option value="5" selected>Средняя</option>
        <option value="15">Большая</option>
      </select>
    </form>
    <canvas width="1300" height="650" style="border:0px solid #fff; margin-top: -80px; margin-left: 100px; cursor:crosshair;" id="tablet">
    </canvas>
canvas
  • 1 个回答
  • 10 Views
Martin Hope
gdhury
Asked: 2020-10-05 02:00:19 +0000 UTC

为什么画布旋转形状上的坐标偏移

  • 1
var canvas=document.getElementById("canvas");
if(canvas.getContext){
var ctx=canvas.getContext("2d");
ctx.rotate(20*Math.PI/180);
ctx.fillRect(90,50,30,20);

}}

矩形很好地偏离了开头的坐标

canvas
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5