使用JavaScript获取图像的一部分 [英] getting part of an image with JavaScript

查看:109
本文介绍了使用JavaScript获取图像的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过JavaScript在 img 标签(带像素)中显示图像的任何部分?

Is it possible to show any part of image in img tag (with pixels) via JavaScript?

我会有一个准备好的大图像(例如32x320像素)和定义的起始位置(X,Y,例如0,32)和宽度/高度(例如32,32),并希望脚本显示第二个(32x32像素)部分主图像。

I would have a prepared big image (e.g. 32x320 pixels) and defined starting position (X,Y , e.g. 0,32) and width/height (e.g. 32,32), and would want the script to show second (32x32 pixel) part of main image.

推荐答案

您可以使用CSS属性并通过JS更改它们。将图像设置为具有所需大小的元素的背景,并使用背景位置调整其位置,以便可以看到正确的部分。有些人称之为 CSS sprites

You could use CSS properties for this and change them via JS. Set the image as a background for an element with your desired size and adjust its position with background-position so that the correct part of it is visible. Some people call it CSS sprites.

这篇关于使用JavaScript获取图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆