この問題に対する最善のアプローチについてアドバイスが必要です:
私は RaphaëlJS に狂ったように恋に落ちました.SVGは私と私のコーディングにとって SVG を実現可能にしてくれました。
.js
ただし、ページにレンダリングするすべての SVG グラフィックのファイルを含める必要はありません。
それで、もっと「動的」なものを見つけることができるかどうかを確認するためにインターネットを探し回ったところ、これを見つけました: (以下は、ここで見つけたコードの編集バージョンです: http://groups.google.com/グループ/raphaeljs/msg/ce59df3d01736a6f )
function parseXML(xml) {
if (window.ActiveXObject && window.GetObject) {
var dom = new ActiveXObject('Microsoft.XMLDOM');
dom.loadXML(xml);
return dom;
}
if (window.DOMParser) {
return new DOMParser().parseFromString(xml, 'text/xml');
throw new Error('No XML parser available');
}
}
(function($) {
$.fn.render_raphaels = function(options) {
var defaults, options, counter, img_path, doc, root, vb, dims, img, node, path, atts, container, new_svg, inline;
defaults = {};
options = $.extend(defaults, options);
counter = 0;
inline = false;
// find all the img's that point to SVGs
$('img[src*="\.svg"]').each(function() {
$(this).fadeOut(1000);
img_path = $(this).attr('src');
if (!$(this).attr('id')) new_svg = true;
if ($(this).hasClass('inline')) inline = true;
container = jQuery('<div/>', {
id: $(this).attr('id') ? $(this).attr('id') : 'svg-' + (counter + 1),
'class': $(this).attr('class') ? $(this).attr('class') : 'svg'
}).hide().insertBefore($(this));
$.get(img_path, null, function(doc) {
doc = parseXML(doc);
root = $(doc).find('svg')[0];
dims = [root.getAttribute('width'), root.getAttribute('height')];
if(new_svg) container.css({ width: dims[0], height: dims[1] });
if(inline) container.css('display', 'inline-block');
img = Raphael(container.attr('id'), parseInt(dims[0]), parseInt(dims[1]));
$(root).find('path').each(function() {
node = this;
path = img.path($(this).attr('d'));
$(['stroke-linejoin','stroke','stroke-miterlimit','stroke-width','fill','stroke-linecap']).each(function() {
if($(node).attr(this.toString())) {
path.attr(this, $(node).attr(this.toString()));
} else {
path.attr(this, 0);
}
});
if($(node).attr('style')) {
atts = $(node).attr('style').split(';');
for(var i=0; i < atts.length; i++) {
bits = atts[i].split(':');
path.attr(bits[0],bits[1]);
}
}
});
}, 'text');
$(this).remove(); // removes the original image after the new one has been redrawn
container.fadeIn(2000);
});
};
})(jQuery);
要するに、これにより、通常のイメージ タグを.svg
グラフィックと共に記述するだけで済み、jQuery プラグインがそれを Raphaël でレンダリングされたバージョンに自動的に置き換えます。
これは、IE などの非 SVG 準拠のブラウザーではうまく機能しますが、実際に既に SVG グラフィックスをサポートしている最新のブラウザーでは、イメージ タグはそのまま (Raphaël なしで) 機能するため、Raphaël がロードされると、既存のイメージがアンロードされ、フェードインします。 Raphaëlバージョン...本質的にちらつきを作成します。新しいバージョンをフェードインしてこれを軽視しようとしましたが、古いバージョンが表示され、隠され、再び表示されるという問題にまだ直面しています。
IE などの問題のあるブラウザーでの望ましい動作と、Safari 4 や Firefox 3 などの最新の標準準拠ブラウザーでの望ましくない動作を調整する方法が必要です。しかし、私が持っていない方法でこれを行いたいです。コーディング方法を大幅に変更する (そもそもプラグインを使用する理由)。
SVG がまだ少し最先端であることは知っていますが、これを回避する方法について何か考えがある人はいますか?
免責事項: 可能な限り、ブラウザーのターゲティングは避けたいと思います... 私は、ブラウザーのハッキングではなく、管理しやすく機能的なワークフロー ソリューションを探しています。
免責事項 2: Flash に基づくソリューションは必要ありません。私はできるだけ「ネイティブ」になりたいと思っており、JavaScript は Flash よりもはるかにネイティブであると考えています。(これが、私がラファエルにとても興奮している理由です。フラッシュから離れることができるからです)。