目次
WordPress の記事に WebGL を埋め込むテスト
テストということで、シザーと画面クリアだけでナイトライダーを試しに作ってみました。
まずは WordPress の記事内に WebGL がうまく埋め込むことができるかどうかを確認することが目的なために、シェーダを使っていないごく簡単なテストになっています。
Custom JS の追加:
JavaScript を埋め込むために functions.php の最後に以下の内容を追加します。
//Custom JS Widget
add_action('admin_menu', 'custom_js_hooks');
add_action('save_post', 'save_custom_js');
add_action('wp_head','insert_custom_js');
function custom_js_hooks() {
add_meta_box('custom_js', 'Custom JS', 'custom_js_input', 'post', 'normal', 'high');
add_meta_box('custom_js', 'Custom JS', 'custom_js_input', 'page', 'normal', 'high');
}
function custom_js_input() {
global $post;
echo '<input type="hidden" name="custom_js_noncename" id="custom_js_noncename" value="'.wp_create_nonce('custom-js').'" />';
echo '<textarea name="custom_js" id="custom_js" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_js',true).'</textarea>';
}
function save_custom_js($post_id) {
if (!wp_verify_nonce($_POST['custom_js_noncename'], 'custom-js')) return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
$custom_js = $_POST['custom_js'];
update_post_meta($post_id, '_custom_js', $custom_js);
}
function insert_custom_js() {
if (is_page() || is_single()) {
if (have_posts()) : while (have_posts()) : the_post();
echo '<script type="text/javascript">'.get_post_meta(get_the_ID(), '_custom_js', true).'</script>';
endwhile; endif;
rewind_posts();
}
}
追加すると投稿画面に「Custom JS」とフィールドが現れます。
Canvas の追加:
WebGL を描画したい場所に Canvas タグを追加します。(テキストエディタで追加)
<canvas id="glCanvas" width="400" height="30" style="background-color: black;">
Your browser doesn't appear to support the HTML5 <code><canvas></code> element.
</canvas>
ビジュアルエディタに切り替えた際に、canvas 領域がどこかがわかるように background-color を black に設定しています。
JavaScript の追加:
「Custom JS」フィールドに JavaScript を追加します。(追加するコードはもちろん WebGL です。)
var canvas = null;
var gl = null;
var fps = 30.0;
var animationFrame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(f) { return window.setTimeout(f, 1000 / fps); };
var xoffset = 0;
var len = 400 / 8;
var step = len / 2;
var animation = function() {
xoffset += step;
if (xoffset >= gl.drawingBufferWidth - len) {
step = - 10;
}
else if (xoffset <= 0) {
step = 10;
}
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.enable(gl.SCISSOR_TEST);
gl.scissor(xoffset, 0, len, 30);
gl.clearColor(1.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.disable(gl.SCISSOR_TEST);
animationFrame(animation);
};
var initialize = function() {
canvas = document.getElementById("glCanvas");
try {
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
}
catch(e) {}
if (gl) {
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
animation();
}
}
window.onload = initialize;
テスト結果:
- 固定ページか、個別投稿のページの場合は期待した動作になるようですが、メインページ等ではヘッダに JavaScript が追加されないため、期待した動作になりません。Custum JS の insert_custom_js を書き換えることで対応することはできますが、複数の投稿ページにそれぞれ JavaScript を追加した場合でも、正しく動作するのかどうかは未確認です。(もっとも、性能面の問題で複数ページで WebGL を動かすのは厳しいので、固定ページか、個別投稿のページでのみ動かすという制限は妥当かもしれません。)
- このテストはシェーダを使っていないので実用的なものではありません。シェーダを使うためにはシェーダも記事に記述できないといけませんが、このままではうまく記述できません。Custum JS のような追加フィールドを functions.php に記述する等の工夫が必要かもしれません。