简体   繁体   English

如何在 canvas 上绘制矩形

[英]How to draw rectangle over canvas

i'm receving from webserver part of image, let say 600px x 600px, which i'm drawing in canvas.我正在从图像的网络服务器部分接收,比如说 600px x 600px,我在 canvas 中绘制。

The real size of image is let say 600px x 1200px(height).图像的实际尺寸可以说是 600 像素 x 1200 像素(高度)。

I need to create scrollbar over canvas.我需要在 canvas 上创建滚动条。 After i will drag scrollbar i will count, how many pixels i've moved scrollbar, send to server information and i will receive other portion of picture (600x600px) but scrolled.在我拖动滚动条后,我将计算,我移动滚动条的像素数,发送到服务器信息,我将收到图片的其他部分(600x600px)但滚动。 The mechanism is done by me, but i need to draw scrollbar over canvas.该机制由我完成,但我需要在 canvas 上绘制滚动条。

<b-card>
  <div id="image">
    <canvas
      id="canvasId"
      ref="canRef"
      @mousedown="clickMe"
      @mouseup="scrollOff"
      @mousemove="scrollMe"
    />
  </div>
</b-card>

How to draw scroll bar on the right side of canvas?如何在canvas右侧绘制滚动条?

First, fetch the canvas element and its context首先,获取 canvas 元素及其上下文

const canvas = document.getElementById('canvasId');
const ctx = canvas.getContext('2d');

Then, draw the rectangle using the stroke() function然后,使用stroke() function 绘制矩形

ctx.beginPath();
ctx.rect(0, 0, 150, 100); //change to the coordinates you want
ctx.stroke();

You can control the color and the width of the line using:您可以使用以下方法控制线条的颜色和宽度:

ctx.lineWidth = 10;
ctx.strokeStyle = '#ff0000';

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

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