- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想要一个在 div 中包含 Canvas 元素的响应式页面。我应该能够简单地将一个 Canvas 元素包裹在另一个 block 元素中,并为该 Canvas 指定一个相对宽度。虽然这个方法在 Chrome、Firefox、Safari、Opera 都有效,但 IE 当然是美中不足!我什至没有尝试比 IE9 更早的版本——这个简单的代码示例在 IE9 中无法按预期工作:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test IE9 Canvas Height</title>
<style>
body {
margin: 0;
padding: 0;
}
#container {
position: relative;
padding-top: 32%;
background-color: #88f;
}
.instruments {
position: absolute;
bottom: 0%;
width: 100%;
padding-bottom: 16%;
background-color: #8f8;
}
#circle {
position: absolute;
left: 42%;
width: 16%;
}
</style>
</head>
<body>
<div id="container">
<div class="instruments">
<canvas id="circle"></canvas>
</div>
</div>
<script>
var canvas = document.getElementById("circle");
var ctx = canvas.getContext("2d");
// Default canvas is 300x150, so make it square
canvas.height = canvas.width;
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height / 2, canvas.width / 2, 0, 2 * Math.PI);
ctx.fillStyle = "red";
ctx.fill();
</script>
</body>
</html>
这个简单的页面应该呈现一个高度为正文宽度 32% 的蓝色 block ,然后有另一个绿色 block 与底部 1/2 重叠。内部 div 内部是一个 Canvas 元素,它没有明确设置高度和宽度——因此它可以在各种窗口/主体宽度下保持流畅。 Canvas 应绘制一个圆形的红色圆圈,高度完全包含在其包含的 div 中。在除 IE9 之外的所有其他浏览器中都能很好地工作(我认为除了 IE10 之外)。
即使我指定了 Canvas 大小,
<canvas id="circle" width="300" height="300"></canvas>
还是不行。
是我做错了什么,还是有更好的方法来实现这个目标?我不想设置像素或点大小的样式或代码,并且更愿意避免 javascript IE hack。谢谢
最佳答案
老实说,您关于 IE 不好的语气又让我不想那么多帮助您。下次再拨。
你在这里的行为有点不确定,因为 canvas
的本质是“拉伸(stretch)”它们的内容,而你的代码中没有任何东西让你的 canvas
保持正方形,所以没有理由让它均匀缩放......你可以通过给 Canvas 一个背景颜色来亲眼看到这个......简短的版本是添加:
height: 100%
到 canvas
的 css 以使其按照您希望的方式工作...希望这对您有所帮助 -ck
更新:
我读了你的评论,你似乎不明白设置 canvas.width
和 canvas.height
只会改变内部“绘图表面”的大小的元素。当您设置元素的样式(默认为 auto
)时,您就是在设置显示表面的尺寸。您遇到的问题是,当其中一个样式维度成员设置为 auto
而另一个是时,您希望浏览器保留 canvas
的内部维度不是,就像它如何与 img
标签一起工作一样。 Chrome 和 Firefox 似乎就是这样。然而,IE 只是让您的实际尺寸通过,所以在您的情况下,就好像您将高度设置为 300px
。我不确定是否有一个“规范正确”的方法来解决这个问题,但似乎所有的实现都集中在您的首选行为上。
无论如何,您的问题的解决方案是使用 img
标签,因为该标签将正常运行并且只需使用数据 uri 将您的 Canvas 图像放入其中:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test IE9 Canvas Height</title>
<style>
body {
margin: 0;
padding: 0;
}
#container {
position: relative;
padding-top: 32%;
background-color: #88f;
}
.instruments {
position: absolute;
bottom: 0%;
width: 100%;
padding-bottom: 16%;
background-color: #8f8;
}
#circle {
position: absolute;
left: 42%;
width: 16%;
}
</style>
</head>
<body>
<div id="container">
<div class="instruments">
<img id="circle" src=""/>
</div>
</div>
<script>
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
// Default canvas is 300x150, so make it square
canvas.height = canvas.width = 300;
ctx.beginPath();
ctx.arc(canvas.width/2, canvas.height/2, canvas.width/2, 0, 2 * Math.PI);
ctx.fillStyle = "red";
ctx.fill();
var img = document.getElementById('circle');
img.src = canvas.toDataURL();
</script>
</body>
</html>
这将为您提供您正在寻找的效果,而无需任何进一步的黑客攻击......
附带说明一下,您使用填充来“调整”具有百分比的元素的 css 技术有点脆弱(就像所有基于 % 的 css 技术一样)。如果我使用基于 % 的 css 布局,我可能会使用这样的东西:
<style>
html, body {
margin: 0;
padding: 0;
height:100%;
}
#container {
height: 33%;
background-color: #88f;
}
.instruments {
height:50%;
background-color: #8f8;
position:relative;
top:50%;
}
#circle {
display:block;
margin:auto;
width:auto;
height:100%;
}
</style>
你可以在这里看到完整的东西(修改过的CSS):http://jsbin.com/ajoTUZA/2/quiet
希望对你有帮助-ck
关于css - 如何在 IE9 中实现响应式 Canvas 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18452154/
我可以使用两种方法添加一个 child ,一种是 Canvas.AddVisualChild(Visual); Canvas.AddLogicalChild(Visual); 我在视觉对象的 Draw
在过去的几周里,我一直在尝试各种方法,试图找到将 BDD 用于依赖于 HTML5 Canvas 元素以及用户与之交互的 Web 应用程序的最佳方法。 我一直在使用 Jasmine 和 Cucumber
我正在尝试完成撤消/重做。我正在使用loadFromJSON(...)从我存储在数组中的 Canvas 状态重新构建 Canvas 。基本上,我的想法是破坏现有的 Canvas 并重新构建 Canva
我正在尝试在 Canvas 上设置简单的放大/缩小功能。我正在使用 KineticJS 处理触摸事件并在 Canvas 中绘图,但无法实现缩放。 KinteicJS 有一个类似的例子,但它们总是在中心
我正在使用 processing.js 在 javascript 中开发一个画笔应用程序 它正在使用 Canvas 对象。我想在 Canvas 的背景中保留一个图像。在前景中画一些东西。在保存时,我只
您好,我想为 discord.js Bot 安装 Canvas 。 当我尝试使用以下命令安装 Canvas 时npm install canvas我收到以下错误: pi@server:~/Bots/D
我正在尝试使用 Canvas 和动力学的组合来构建填充图案,但在尝试获得连续线时遇到了问题。 此 jsfiddle显示了到目前为止我所拥有的,但是因为我的重复模式是正方形,角会影响线条,我尝试使用 l
我正在开发一个 webassembly 程序。 我可以使用 emscripten_set_canvas_size 设置 Canvas 大小(我一直读到我需要切换到新的 API,因为这个 API 会贬值
您好,我已经为第一个 Canvas 中的第一个图像创建了一个圆形表单,但我没有成功使用第一个 Canvas 的 dataURL 并将其添加到第二个 Canvas 中。 这是我的 fiddle :htt
问题在于不同浏览器之间的不一致。 使用Dart Chrome,JS Chrome,JS Opera运行 双击可以进入和退出全屏 m_oCanvas.width =(window.screen.widt
我正在使用Flutter框架和Dart开发图像编辑器,因此无法将矩阵滤镜应用于 Canvas 。 我正在尝试使用“Paint”类和“canvas.drawPaint(paint)”函数将矩阵过滤器应用
如果在已经具有非整数比例因子的 Canvas 上绘制图像,我会遇到 Canvas 上下文drawImage()方法的问题。似乎这样的图像以一种奇怪的方式被剪切(有时图像的最右边的部分被剪切,有时是最底
Canvas 的“宽度”属性值有限制吗? 在下面的示例中,我在 ScrolledWindow 中创建一个 Canvas。 # Packages package require BWidget # Ma
我正在尝试制作类似于 this article 底部的效果的文本效果 我建议的方法是: 制作两个 Canvas ,一个是可见的,另一个是不可见的我用它作为缓冲区。 在缓冲区 Canvas 上绘制一些文
例如var new = canvas.toDataURL("image/png"); 我希望这个新变量中存在的 base64 显示到存在的第二个 Canvas 元素中。但是它不使用 drawimage
有人有使用这两个 Node.js 库中的一个或两个的经验吗?很想知道每个人的成功或困难。 最佳答案 LearnBoost是社区中最多产的 Node 模块开发人员之一,因此我选择使用 node-canv
如何知道 Canvas 运行的是“WebGL”还是普通 Canvas ? 通过检查源代码,我发现这两种情况都是 Canvas 。 最佳答案 这真的取决于你想如何去发现。 例如你可以这样调用 `getC
在 Canvas 上绘图非常好。甚至橡皮擦也能正常工作。问题是,当 Canvas 保存为图像时,它绘制的是黑线而不是橡皮擦。 为了更好地理解,我添加了屏幕截图和代码。 1。在删除绘图时 - 一个。源代
我正在尝试为 Canvas 附加鼠标悬停和鼠标移出事件: 默认 Canvas 是函数drawcircle的 Canvas 。 如果用户越过 Canvas ,应将其更改为drawEllipse的 Can
我正在使用 Three.js 构建一个简单的 2D 游戏。我只使用世界的 X 和 Y 位置来移动对象,将它们的 z 位置保留为零。我使用禁用旋转的 TrackballControls,以允许使用右键单
我是一名优秀的程序员,十分优秀!