drawImage and resize to Canvas(drawImage 并调整大小为 Canvas)
问题描述
我有一个 1836 x 3264 的图像,我想 drawImage()
到画布上并将大小调整为 739 x 1162.
I have an image which is 1836 x 3264 I want to drawImage()
to canvas and resize to 739 x 1162.
阅读文档后,我认为这可以通过以下方式完成:
After reading the documentation I thought this could be accomplished with the following:
ctx.drawImage(image, 0, 0, 739, 1162);
我也试过了:
ctx.drawImage(image, 0, 0, 1836, 3264, 0, 0, 739, 1162);
两者都只显示完整图像的一小部分,而不是缩小它.
Both show only a small part of the full image instead of shrinking it down.
如何传递值以从 1836 x 3264 -> 739 x 1162 调整大小?
How do I pass through the values to resize from 1836 x 3264 -> 739 x 1162 ?
推荐答案
你看到的东西是正确的,所以你可能会仔细检查某个地方的错字.
What you have looks correct, so you might double-check for a typo somewhere.
[额外的想法:你的图像真的是 1836x3264 而不是 3264x1836.]
[additional thought: Is your image really 1836x3264 and not 3264x1836.]
这是工作代码和小提琴:http://jsfiddle.net/m1erickson/MLGr4/
Here is working code and a Fiddle: http://jsfiddle.net/m1erickson/MLGr4/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="aHR0cDovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnkubWluLmpz"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
img=new Image();
img.onload=function(){
canvas.width=400;
canvas.height=300;
ctx.drawImage(img,0,0,img.width,img.height,0,0,400,300);
}
img.src="aHR0cDovL3d3dy5vbmVzdG9wd2VibWFzdGVycy5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTEvMDYvZWl0YWktYnJpZGdlLmpwZw==";
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=100 height=100></canvas>
</body>
</html>
这篇关于drawImage 并调整大小为 Canvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:drawImage 并调整大小为 Canvas


- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06