迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中使用 HTML Canvas 调整图像大小

作者:迹忆客 最近更新:2024/03/19 浏览次数:

图形用于描述图像的各个方面,这是任何 Web 应用程序的重要组成部分。HTML 提供了两种创建图形的方法。第一个是 canvas,另一个是 SVG。在今天的文章中,我们将学习如何创建图形,尤其是如何使用 canvas 和 JavaScript 在 HTML 中绘制图片。


使用 JavaScript 在 HTML 中通过 canvas 调整图像大小

Canvas 是一种标准的 HTML 元素,用于在 Web 应用程序中绘制图形。它只不过是页面上没有边框或内容的矩形区域。用户可以使用这个矩形区域来绘制图形。

在画布上呈现的图形不同于普通的 HTML 和 CSS 样式。整个画布及其包含的所有图形都被视为单个 DOM 元素。

HTML 中 canvas 的方法

drawImage() 的语法

context.drawImage(
    $image, $sx, $sy, $sWidth, $sHeight, $dx, $dy, $dWidth, $dHeight);

参数

  • $image:这是一个强制参数,指定需要绘制到画布中的图像源。图像源可以是 CSSImageValueHTMLImageElementSVGImageElement 等。
  • $sx:这是一个可选参数,用于指定源图像的 X 或水平坐标。它将被测量为从左上角开始绘制的矩形以绘制到目标图像中。
  • $sy:这是一个可选参数,用于指定源图像的 Y 或垂直坐标。它将被测量为从左上角开始绘制的矩形以绘制到目标图像中。
  • $sWidth:它是一个可选参数,指定源图像的宽度。它将被测量为从左上角开始绘制的矩形以绘制到目标图像中。
  • $sHeight:这是一个可选参数,用于指定源图像的高度。它将被测量为从左上角开始绘制的矩形以绘制到目标图像中。
  • $dx:这是一个强制参数,指定目标图像的 X 或水平坐标。它将被测量为从放置源图像的左上角开始的矩形。
  • $dy:这是一个强制参数,指定目标图像的 Y 或垂直坐标。它将被测量为从放置源图像的左上角开始的矩形。
  • $dWidth:这是一个可选参数,用于指定目标图像的宽度。它将被测量为从左上角开始的矩形。如果未指定值,则默认宽度为源图像的宽度。
  • $dHeight:这是一个可选参数,指定目标图像的高度。它将被测量为从左上角开始的矩形。如果未指定,则默认高度为源图像的高度。

使用 JavaScript Canvas 调整图像大小的步骤

  • 获取 Canvas 的上下文。
  • 设置画布的高度。
  • 创建 mew 画布元素以执行调整大小并获取其上下文。
  • 设置新创建的 Canvas 的宽度和高度。
  • 在新画布上绘制图像。
  • 使用新创建的 Canvas 在最终 Canvas 上绘制图像。

示例代码:

<img src="https://images.pexels.com/photos/3408744/pexels-photo-3408744.jpeg?auto=compress&cs=tinysrgb&dpr=2" width="500" height="400">
<canvas id="canvas" width=1000></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
img.src =
    'https://images.pexels.com/photos/3408744/pexels-photo-3408744.jpeg?auto=compress&cs=tinysrgb&dpr=2';

img.onload = function() {
  // set height proportional to destination image
  canvas.height = canvas.width * (img.height / img.width);
  // step 1 - resize to 75%
  const oc = document.createElement('canvas');
  const octx = oc.getContext('2d');
  // Set the width & height to 75% of image
  oc.width = img.width * 0.75;
  oc.height = img.height * 0.75;
  // step 2, resize to temporary size
  octx.drawImage(img, 0, 0, oc.width, oc.height);
  // step 3, resize to final size
  ctx.drawImage(
      oc, 0, 0, oc.width * 0.75, oc.height * 0.75, 0, 0, canvas.width,
      canvas.height);
}

输出:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便