html2canvas遇到的坑,图片模糊,报Error loading image

最近业务上遇到需要把div的内容生成图片,查了一下,比较好的js插件是html2canvas,

用下来也确实比较简单,但遇到一些坑,比如生成的图片比较模糊,或者报Error loading image 

1.一开始用的0.5.0-beta4的版本,生成的图片很模糊,网上一大堆解决方案,但其实只要用新一点的版本就能解决问题。

2.于是我找了最新的版本下载地址v1.0.0-rc.5,虽然生成的图片清晰了,但发现会报Error loading image 。

3.最终解决方案是用了v1.0.0-alpha.9这个版本,问题解决,图片清晰也不报错了。

以下是测试代码

<html>
<head>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="html2canvas.min.js"></script>
<link rel="stylesheet"
	href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script
	src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

	<!-- 按钮触发模态框 -->
	<button class="btn btn-primary btn-lg" onclick="m()">开始演示模态框</button>
	<!-- 模态框(Modal) -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">×</button>
					<h4 class="modal-title" id="myModalLabel">模态框(Modal)标题</h4>
				</div>
				<div class="modal-body">
					<div style="background: #fff;">
						<div>1</div>
						<div>2</div>
						<div>3</div>
						<div>4</div>
					</div>
					<div id="imgDiv" style="position: absolute; background: #eee;">
						<img id="imgId" class="imgDiv_img" src="" style="width: 100%;" />
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭
					</button>
					<button type="button" class="btn btn-primary" onclick="d()">
						生成图片</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	<script type="text/javascript">
		function m() {
			$("#myModal").modal("show");
		}

		function d() {

			html2canvas(document.querySelector("#myModal")).then(
					function(canvas) {
						var imgUri = canvas.toDataURL("image/jpeg", 1); // 获取生成的图片的url
						$(".imgDiv_img").attr("src", imgUri)
					})
		}
	</script>
</body>
</html>

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
MSR Identity Toolbox: A Matlab Toolbox for Speaker Recognition Research Version 1.0 Seyed Omid Sadjadi, Malcolm Slaney, and Larry Heck Microsoft Research, Conversational Systems Research Center (CSRC) [email protected], {mslaney,larry.heck}@microsoft.com This report serves as a user manual for the tools available in the Microsoft Research (MSR) Identity Toolbox. This toolbox contains a collection of Matlab tools and routines that can be used for research and development in speaker recognition. It provides researchers with a test bed for developing new front-end and back-end techniques, allowing replicable evaluation of new advancements. It will also help newcomers in the field by lowering the “barrier to entry”, enabling them to quickly build baseline systems for their experiments. Although the focus of this toolbox is on speaker recognition, it can also be used for other speech related applications such as language, dialect and accent identification. In recent years, the design of robust and effective speaker recognition algorithms has attracted significant research effort from academic and commercial institutions. Speaker recognition has evolved substantially over the past 40 years; from discrete vector quantization (VQ) based systems to adapted Gaussian mixture model (GMM) solutions, and more recently to factor analysis based Eigenvoice (i-vector) frameworks. The Identity Toolbox provides tools that implement both the conventional GMM-UBM and state-of-the-art i-vector based speaker recognition strategies. A speaker recognition system includes two primary components: a front-end and a back-end. The front-end transforms acoustic waveforms into more compact and less redundant representations called acoustic features. Cepstral features are most often used for speaker recognition. It is practical to only retain the high signal-to-noise ratio (SNR) regions of the waveform, therefore there is also a need for a speech activity detector (SAD) in the fr
html2canvas是一个用于将HTML元素转换成图像的JavaScript库。但是,有时候在使用html2canvas时,生成的图像可能会出现模糊的问题。 出现这种情况的原因有很多,以下是几个可能导致html2canvas图像模糊的原因: 1. 页面缩放问题:如果页面进行了缩放,html2canvas可能会将缩放后的图像进行截取,导致图像模糊。可以尝试将页面还原到默认的缩放比例。 2. 图片大小问题:如果将较大的HTML元素转换为图像,并且图像显示的尺寸较小,html2canvas可能会在缩放图像时导致模糊。可以尝试将图像显示尺寸与原始元素尺寸保持一致。 3. CSS样式问题:有些CSS样式可能会影响html2canvas生成的图像质量。例如,使用CSS的transform属性进行旋转、缩放等效果可能会导致图像模糊。可以尝试去除或优化这些CSS样式。 4. 字体渲染问题:如果html2canvas无法正确地渲染页面中的特定字体,则可能会导致图像模糊。可以尝试加载相应的字体文件或使用默认的通用字体。 5. html2canvas库版本问题:如果使用的是过时或有已知问题的html2canvas库版本,也可能导致图像模糊。可以尝试更新到最新的版本。 要解决html2canvas生成的图像模糊问题,可以尝试上述提到的解决方法。另外,还可以查阅html2canvas官方文档和开发者社区,了解是否有其他用户遇到了类似的问题,并尝试寻找解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值