- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
SVG 很丑,请查看我的:
HTML:
<svg version="1.1" class="overlap-svg" id="alaska"></svg>
<svg version="1.1" class="overlap-svg" id="grid"></svg>
CSS:
.overlap-svg {
position: absolute;
left:0;
top: 0;
}
如果我们重叠这 2 个 svg,JS 函数是什么来突出显示其中包含阿拉斯加(红色)部分的 svg 圆圈?
查看下面的描述以获取更多信息
我如何转换它:
像这样:
如果阿拉斯加(红色)的任何部分在圆圈区域内,则圆圈应填充为红色。
请再次查看我上面的 JSFiddle 链接。
最佳答案
您可以获取 svg 并将其加载到 Canvas 元素中。获取该元素,因为它是一个 Canvas 元素,所以您可以获得它的像素数组。
可以通过从适当调整大小的 Canvas 的像素构建网格来构建您的圆形抽象。
首先是一个助手:网格管理器。
function GridManager(configIn) {
var gm_ = {};
gm_.config = {
'gridWidth': 10,
'gridHeight': 10,
'gridCellWidth': 10,
'gridCellHeight': 10,
'gridHeight': 100,
'dataSrc': []
};
// Load new config over defaults
for (var property in configIn) {
gm_.config[property] = configIn[property];
}
/**
* Creates an array using the module's config building a 2d data array
* from a flat array. Loops over GridManager.config.dataSrc
*
* Render a checkerboard pattern:
* GridManager.config.dataSrc = ["#"," "]
*
* Render you can load a image by passing in its full pixel array,
* provided image height and width match GridManager.config.gridHeight
* and GridManager.config.gridWidth.
*/
gm_.createGridSrc = function() {
var height = this.config.gridHeight;
var width = this.config.gridWidth;
var output = [];
for (var i = 0; i < height; i++) {
output[i] = [];
for (var ii = 0; ii < width; ii++) {
if (this.config.dataSrc !== undefined) {
var dataSrc = this.config.dataSrc;
output[i][ii] = dataSrc[i*width + ii % dataSrc.length];
}
}
}
return output;
};
/**
* Creates a SVG with a grid of circles based on
* GridManager.config.dataSrc.
*
* This is where you can customize GridManager output.
*/
gm_.createSvgGrid = function() {
var cellWidth = this.config.gridCellWidth;
var cellHeight = this.config.gridCellHeight;
var svgWidth = 1000;
var svgHeight = 1000;
var radius = 3
var cellOffset = radius / 2;
//create svg
var xmlns = 'http://www.w3.org/2000/svg';
var svgElem = document.createElementNS (xmlns, 'svg');
svgElem.setAttributeNS (null, 'viewBox', '0 0 ' + svgWidth + ' ' + svgHeight);
svgElem.setAttributeNS (null, 'width', svgWidth);
svgElem.setAttributeNS (null, 'height', svgHeight);
svgElem.style.display = 'block';
//create wrapper path
var g = document.createElementNS (xmlns, 'g');
svgElem.appendChild (g);
//create grid
var data = this.createGridSrc();
var count = 0;
for (var i = data.length - 1; i >= 0; i--) {
for (var ii = data[i].length - 1; ii >= 0; ii--) {
// This svgHeight and svgWidth subtraction here flips the image over
// perhaps this should be accomplished elsewhere.
var y = svgHeight - (cellHeight * i) - cellOffset;
var x = svgWidth - (cellWidth * ii) - cellOffset;
var cell = document.createElementNS (xmlns, 'circle');
var template = data[i][ii];
// Machine has averaged the amount of fill per pixel
// from 0 - 255, so you can filter just the red pixels like this
// over a certain strength.
if (template[0] > 10 ) {
cell.setAttributeNS (null, 'fill', '#ff0000');
// Consider stashing refs to these in this.groups['red'] or something
// similar
} else {
cell.setAttributeNS (null, 'fill', 'none');
}
cell.setAttributeNS (null, 'stroke', '#000000');
cell.setAttributeNS (null, 'stroke-miterlimit', '#10');
cell.setAttributeNS (null, 'cx', x);
cell.setAttributeNS (null, 'cy', y);
cell.setAttributeNS (null, 'r', radius);
g.appendChild (cell);
}
}
return svgElem;
}
return gm_;
}
然后在 main.js 中
var wrapper = document.getElementById('wrapper');
var mySVG = document.getElementById('alaska').outerHTML;
mySVG = mySVG.slice(0, 4) + ' height="100" ' + mySVG.slice(4);
// Create a Data URI based on the #alaska element.
var mySrc = 'data:image/svg+xml;base64,' + window.btoa(mySVG);
// Create a new image to do our resizing and capture our pixel data from.
var source = new Image();
source.onload = function() {
var svgRasterStage = document.createElement('canvas');
svgRasterStage.width = 1000;
svgRasterStage.height = 1000;
svgRasterStage.classList.add('hidden');
// You may not need this at all, I didn't test it.
wrapper.appendChild(svgRasterStage);
// Get drawing context for the Canvas
var svgRasterStageContext = svgRasterStage.getContext('2d');
// Draw the SVG to the stage.
svgRasterStageContext.drawImage(source, 0, 0);
// We can now get array of rgba pixels all concatinated together:
// [ r, g, b, a, r, g, b, a, (...) r, g, b, a, r, g, b, a]
var rgbaConcat = svgRasterStageContext.getImageData(0, 0, 100, 100).data;
// Which sucks, so here's a way to convert them to pixels that we can
// use with GridManager.createSvgGrid.
var pixels = [];
var count = 0;
// NOTE: this is a for with a weird step: i=i-4. i-4 is an infinte loop.
// anything else just jumbles the pixels.
for (var i = rgbaConcat.length - 1; i >= 0; i=i-4) {
var r = rgbaConcat[i - 0];
var g = rgbaConcat[i - 1];
var b = rgbaConcat[i - 2];
var a = rgbaConcat[i - 3];
pixels.push([r, g, b, a]);
}
// We create our GridManager (finally).
var gm = new GridManager({
'gridWidth': 100,
'gridHeight': 100,
'dataSrc': pixels
});
// And let her rip!
wrapper.appendChild(gm.createSvgGrid());
}
关于javascript - 将复杂的 svg 形状转换为圆形抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33158918/
我对 java 中的抽象有点困惑。 我查了很多页面说抽象就是数据隐藏(隐藏实现)。 我对抽象的理解是它是“部分实现”。只需在抽象类/接口(interface)中定义您需要的内容,然后扩展/实现它们并添
我是 Ruby 的新手,主要来自 C# 和 ActionScript 3(以及其他语言)。我对抽象功能很好奇。具体来说,包装和抽象 Ruby 的 FTP 和 SFTP 库。 我四处搜索,发现了一个名为
目录 Java基础知识(抽象) 抽象 抽象定义 abstract的使用 定义抽象类
这个月我花了一些时间与 Emacs Lisp 进行斗争,试图获得更好地满足我需求的自动缩进。令人惊讶的是,大多数缩进代码是多么低级。我只看到了很少的抽象,例如 搜索不在字符串或注释中的第一个正则表达式
我有以下内容: public abstract class Foo{ //contents of Foo // ... public class Bar extends
我有三个类(class)(A 类、B 类和 C 类)。 类 A 调用 B 的实例并运行 start()。 B类扩展了Thread,因此当调用start()时,run()方法中的任何内容都会被执行。 在
这个问题已经有答案了: Calling a subclass method from superclass (5 个回答) 已关闭 7 年前。 Klasse1是一个抽象类,有一个 foo()方法。 K
我有一个这样的函数: def test(): x = "3" # In actual code, this is computed if x is None: retu
我有两个基类之间的关系: public abstract class RecruiterBase { // Properties declare here // Constructors de
这是我第一次发帖,但我遇到了很多问题。我目前有一个带有标题的 AbstractDevice 类: public abstract class AbstractDevice> implements De
我有一个 MotorDefinition 类和一个名为 Motor 的抽象类: class MotorDefinition { public: MotorDefinition(int p1,
是否有任何方法可以在这种代码(sass)中制定 css 的抽象规则: #cottage-image-gallery input:nth-of-type(1):checked ~ label:nth-o
是否可以声明一个已知的基类型并允许传输所有派生类型? [ServiceContract] public interface IService { [OperationContract]
我目前正在为基于 Java 的文本游戏开发角色生成机制,但我遇到了一个问题,看不出哪里出了问题。我有一个“Character”类,它是抽象的,然后是另一个类“NPCharacter”,它是建立在这个之
抱歉,标题令人困惑。不太确定如何表达它,这可能是问题所在! 我正在寻找一个好的抽象来用于涉及并发线程的情况。 我已经接近了,但还不是很清楚。 稍微简化一下,我在 Android 手机上收集了两种传感器
提前感谢您阅读本文。我不完全理解如何/何时使用摘要,所以我试图在我从事的每个项目中考虑它,看看它是否会在某一天全部点击 Smile | :) 此外,可访问性级别(私有(private)、 protec
我正在探索用于生成 Web 内容的 XML -> XSLT -> HTML 模因。我的 XSLT 经验很少。 我很好奇 XSLT 中有哪些机制可用于处理抽象或“重构”。 例如,使用通用 HTML 和服
在这些谈话中 Nicholas Zakas和 Addy Osmani他们讨论了在构建大型 Javascript 应用程序时将外观模式用作沙箱的想法,以便将应用程序与底层基础库分离。 这种解耦理论上允许
我使用C++和CUDA/C,想为特定问题编写代码,但遇到了一个非常棘手的简化问题。 我在并行编程方面的经验不容忽视,但相当有限,我无法完全预见到此问题的特殊性。 我怀疑是否有一种方便甚至“轻松”的方式
假设我有: trait A class B extends A class C extends A 有没有办法配置类型参数: class Foo[AType <: A with canCreateIn
我是一名优秀的程序员,十分优秀!