背景にSVGの長方形、前景にHTMLテキストで構成されるボタンのようなオブジェクトを取得したいと思います。Raphael SVGライブラリは、SVG要素を作成、変更、およびアニメーション化するための優れた可能性を提供します。
Raphaelオブジェクトに沿って/上にDIV要素でHTMLテキストを描画するソリューションを見つけました。Raphaelのアニメーションメソッドを使用して、テキストを背景とともに移動させるにはどうすればよいですか?
基本的な2D移動にのみ関心があり、回転や変形はありません... RaphaelでサポートされているSVGテキスト要素を使用できることはわかっていますが、これではテキストの折り返し、テキストのスタイル設定(css)はできません。アニメーション中にアニメーションオブジェクトの座標を取得する方法を見つけようとしましたが、失敗しました。
上記の例madebyKelleyReynoldsに触発されたcoffeescriptのサンプルコードを次に示します。私の問題は、背景の動きをオーバーレイするdivと同期させる方法です。
nodeBox = (paper, params, attrs) ->
params = params or {}
attrs = attrs or {}
@paper = paper
@x = params.x or 0
@y = params.y or 0
@width = params.width or @paper.width
@height = params.height or @paper.height
@xMargin = params.xMargin or 5
@yMargin = params.yMargin or 5
@rounding = params.rounding or 0
@backgBox = this.paper.rect(@x-@xMargin, @y-@yMargin, @width+2*@xMargin, @height+2*@yMargin, this.rounding).attr(attrs)
containerId = @backgBox.node.parentNode.parentNode.id
containerId = containerId or @backgBox.node.parentNode.parentNode.parentNode.id
@container = jQuery('#' + containerId)
@div = jQuery('<div style="position: absolute; overflow: auto; width: 0; height: 0;"></div>').insertAfter(@container)
jQuery(document).bind('ready', this, (event) ->
event.data.update()
)
jQuery(window).bind('resize', this, (event) ->
event.data.update()
)
return this
# update function
nodeBox::update = () ->
offset = @container.offset()
@div.css(
'top': (@y + (@rounding) + (offset.top)) + 'px',
'left': (@x + (@rounding) + (offset.left)) + 'px',
'height': (@height - (@rounding*2) + 'px'),
'width': (@width - (@rounding*2) + 'px')
)
# animate function
nodeBox::animate = (attrs, duration, type) ->
@backgBox.animate(attrs, duration, type)
###
Animation of the div ???
###
$(document).ready ->
paper = new Raphael document.getElementById('canvas_container'), '100%', '100%'
node1 = new nodeBox(paper, x:200, y:200, width:200, height:60, rounding: 10, xMargin: 8, yMargin: 4, 'showBorder': true).attr(fill: 'lightblue', stroke: '#3b4449', 'stroke-width': 3)
node1.div.html('This is some crazy content that goes inside of that box that will wrap around.')
node1.div.css('font-family': 'Helvetica','font-size': 16, 'text-align': 'justify')
# animate :
node1.animate({x: moveX}, 1000, 'bounce')