14 January 2003, 翻訳更新: 2010/04/29

18 スクリプト


目次


  .

18.1 スクリプト言語の指定

18.1.1 既定のスクリプト言語の指定

'svg' 要素の contentScriptType 属性は文書片において既定のものとなるスクリプト言語を指定する。

The contentScriptType attribute on the 'svg' element specifies the default scripting language for the given document fragment.

contentScriptType = "content-type"
エラータ :次を修正)
文書SVG 文書片において既定のものとなるスクリプト言語を特定する。この属性は イベント属性 に与えられる文字列値の処理に利用される既定のスクリプト言語を設定する。 この言語は、自身のスクリプト言語を指定しないスクリプトのすべてのインスタンスに利用されなければならない。content-type には [RFC2045] によるメディア型を指定する。既定値は "text/ecmascript""application/ecmascript" である(この仕様の発行時においてはまだ "text/ecmascript""application/ecmascript" は ECMAScript のメディア型としてまだ登録済みでないことに注意。別のものが登録されない限り既定のものであり続けるであろうが)。
アニメーション:不可
Identifies the default scripting language for the given document. This attribute sets the scripting language used to process the value strings in event attributes. The value content-type specifies a media type, per [ RFC2045]. The default value is "text/ecmascript" (Note that at the time of publication "text/ecmascript" has not been registered as the media type for ECMAScript. It will continue to be the default unless an alternative is registered).
Animatable: no.

18.1.2 スクリプト言語の局所的宣言

'script' 要素の type 属性 を指定することにより、各 'script' 要素ごとに個別にスクリプト言語を指定することが可能である。

It is also possible to specify the scripting language for each individual 'script' element by specifying a type attribute on the 'script' element.

18.2 'script' 要素

'script' 要素は HTML の 'script' 要素と同じであり、すなわちスクリプト(例えば ECMAScript )の置き場を提供する。 'script' 要素内で定義される関数は現在の文書全体に渡って「大域的( global )」な有効範囲を持つ。

A 'script' element is equivalent to the 'script' element in HTML and thus is the place for scripts (e.g., ECMAScript). Any functions defined within any 'script' element have a "global" scope across the entire current document.

下の Example script01 では関数 circle_click が定義されており、 'circle' 要素の onclick イベント属性から呼び出される。下の図左は初期状態の画像である。下の図右は円形をクリックした後の結果を示している。

Example script01 defines a function circle_click which is called by the onclick event attribute on the 'circle' element. The drawing below on the left is the initial image. The drawing below on the right shows the result after clicking on the circle.

この例は説明の目的で onclick イベント属性を利用しているに過ぎないことに注意。例においてはマウスと同じ振る舞いをする入力装置の存在を仮定しているが、常に存在しているとは限らない。可能な限り広範囲の利用者をサポートするには、 onclick イベント属性のかわりに onactivate イベント属性が用いられるべきである。

Note that this example demonstrates the use of the onclick event attribute for explanatory purposes. The example presupposes the presence of an input device with the same behavioral characteristics as a mouse, which will not always be the case. To support the widest range of users, the onactivate event attribute should be used instead of the onclick event attribute.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="6cm" height="5cm" viewBox="0 0 600 500"
     xmlns="http://www.w3.org/2000/svg" version="1.1">
  <desc>Example script01 - onclick イベントからの
  ECMAScript 関数の呼び出し
  </desc>
  <!-- クリックごとに半径を変化させる ECMAScript -->
  <script type="text/ecmascript"> <![CDATA[
    function circle_click(evt) {
      var circle = evt.target;
      var currentRadius = circle.getAttribute("r");
      if (currentRadius == 100)
        circle.setAttribute("r", currentRadius*2);
      else
        circle.setAttribute("r", currentRadius*0.5);
    }
  ]]> </script>
  <!-- 描画領域の外形線を青色の線でなぞる -->
  <rect x="1" y="1" width="598" height="498" fill="none" stroke="blue"/>
  <!-- 各 click イベントごとに発動 -->
  <circle onclick="circle_click(evt)" cx="300" cy="225" r="100"
          fill="red"/>
  <text x="300" y="480" 
        font-family="Verdana" font-size="35" text-anchor="middle">
    Click on circle to change its size
  </text>
</svg>
Example script01
Example script01 - invoke an ECMAScript function from an onclick event - before first click     Example script01-AfterClick - invoke an ECMAScript function from an onclick event - after first click

この例を SVG で見る( SVG 対応ブラウザのみ)
 

<!ENTITY % SVG.script.extra.content "" >
<!ENTITY % SVG.script.element "INCLUDE" >
<![%SVG.script.element;[
<!ENTITY % SVG.script.content
    "( #PCDATA %SVG.script.extra.content; )*"
>
<!ELEMENT %SVG.script.qname; %SVG.script.content; >
<!-- end of SVG.script.element -->]]>
<!ENTITY % SVG.script.attlist "INCLUDE" >
<![%SVG.script.attlist;[
<!ATTLIST %SVG.script.qname;
    %SVG.Core.attrib;
    %SVG.XLink.attrib;
    %SVG.External.attrib;
    type %ContentType.datatype; #REQUIRED#IMPLIED
>

属性定義:

type = "content-type"
エラータ :次を修正。これに伴い上の DTD も修正。)
'script' 要素のスクリプト言語を指示する。値 content-type は [RFC2045] によるメディア型を指定する。 'script' 要素のプログラミング言語を指示する。値 content-typeMultipurpose Internet Mail Extensions (MIME) PartTwo [RFC2046] によるメディア型を指定する。 'type' が与えられなかった場合、 svg 要素の 'contentScriptType' の値が用いられるものとする(すなわちデフォルトで "application/ecmascript" [RFC4329] )。 script 要素が 最も外側の svg 要素 の外側にある状況で 'type' が与えられていなかった場合、 'type' のデフォルトは "application/ecmascript" [RFC4329] でなければならない。
アニメーション:不可
Identifies the scripting language for the given 'script' element. The value content-type specifies a media type, per [ RFC2045]. Animatable: no.
xlink:href = "<uri>"
エラータ :この項目を追加)
スクリプトコードを含む外部リソースへの URI 参照。
アニメーション:不可

18.3 イベント処理

次のいずれかが生じる時、イベントによりスクリプトが実行される:

Events can cause scripts to execute when either of the following has occurred:

関連する仕様:

Related sections of the spec:

18.4 イベント属性

次のイベント属性は多くの SVG 要素で利用できる。

The following event attributes are available on many SVG elements.

SVG 言語の一部としてのイベントの完全な一覧とそれらのイベントに対する SVG DOM および説明は サポートされているイベントの完全な一覧 を見よ。

The complete list of events that are part of the SVG language and SVG DOM and descriptions of those events is provided in Complete list of supported events.

グラフィックス要素とコンテナ要素のイベント属性

<!ENTITY % SVG.onfocusin.attrib
    "onfocusin %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onfocusout.attrib
    "onfocusout %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onactivate.attrib
    "onactivate %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onclick.attrib
    "onclick %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onmousedown.attrib
    "onmousedown %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onmouseup.attrib
    "onmouseup %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onmouseover.attrib
    "onmouseover %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onmousemove.attrib
    "onmousemove %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onmouseout.attrib
    "onmouseout %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onload.attrib
    "onload %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.GraphicalEvents.extra.attrib "" >
<!ENTITY % SVG.GraphicalEvents.attrib
    "%SVG.onfocusin.attrib;
     %SVG.onfocusout.attrib;
     %SVG.onactivate.attrib;
     %SVG.onclick.attrib;
     %SVG.onmousedown.attrib;
     %SVG.onmouseup.attrib;
     %SVG.onmouseover.attrib;
     %SVG.onmousemove.attrib;
     %SVG.onmouseout.attrib;
     %SVG.onload.attrib;
     %SVG.GraphicalEvents.extra.attrib;"
>

文書レベルのイベント属性

<!ENTITY % SVG.onunload.attrib
    "onunload %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onabort.attrib
    "onabort %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onerror.attrib
    "onerror %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onresize.attrib
    "onresize %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onscroll.attrib
    "onscroll %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onzoom.attrib
    "onzoom %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.DocumentEvents.extra.attrib "" >
<!ENTITY % SVG.DocumentEvents.attrib
    "%SVG.onunload.attrib;
     %SVG.onabort.attrib;
     %SVG.onerror.attrib;
     %SVG.onresize.attrib;
     %SVG.onscroll.attrib;
     %SVG.onzoom.attrib;
     %SVG.DocumentEvents.extra.attrib;"
>

アニメーションイベント属性

<!ENTITY % SVG.onbegin.attrib
    "onbegin %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onend.attrib
    "onend %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onrepeat.attrib
    "onrepeat %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.onload.attrib
    "onload %Script.datatype; #IMPLIED"
>
<!ENTITY % SVG.AnimationEvents.extra.attrib "" >
<!ENTITY % SVG.AnimationEvents.attrib
    "%SVG.onbegin.attrib;
     %SVG.onend.attrib;
     %SVG.onrepeat.attrib;
     %SVG.onload.attrib;
     %SVG.AnimationEvents.extra.attrib;"
>

アニメーション:不可

Animatable: no.

 

18.5 Scripting モジュール

要素 属性 内容モデル
script Core.attrib, XLink.attrib, type #PCDATA

18.5.1 Scripting 内容セット

Scripting モジュールは Script.class 内容セットを定義する。

内容セット名 内容セットに含まれる要素
Script.class script

18.6 DOM インターフェース

次に挙げるインターフェースが以下で定義される: SVGScriptElement, SVGEvent, SVGZoomEvent


インターフェース SVGScriptElement

SVGScriptElement インターフェースは 'script' 要素に対応する。


IDL 定義
interface SVGScriptElement : 
                SVGElement,
                SVGURIReference,
                SVGExternalResourcesRequired { 
           attribute DOMString type;
                       // raises DOMException on setting
};

属性
DOMString type
'script' 要素の type 属性に対応する。
設定時の例外
DOMException
NO_MODIFICATION_ALLOWED_ERR :読み出し専用の属性値の変更を試みたときにレイズされる。

インターフェース SVGEvent

SVG イベントセットは SVG で利用できる固有のイベント型のリストを含む。

SVG イベントセットが DOM 実装に実装されているかどうかの判別には、 DOMImplementation インターフェースの hasFeature が利用できる。このイベントセットに対する特色機能文字列は "SVGEvents" である。この文字列は createEvent メソッドでも利用される。

SVG イベントは文脈情報の受け渡しに基本 DOM イベントインターフェースを利用する。

以下に挙げるイベント型が生じ得る(浮上 = Bubbles, 取消 = Cancelable, 文脈情報 = Context Info ):

SVGLoad
SVGLoad イベント を見よ。
SVGUnload
SVGUnload イベント を見よ。
SVGAbort
SVGAbort イベント を見よ。
SVGError
SVGError イベント を見よ。
SVGResize
SVGResize イベント を見よ。
SVGScroll
SVGScroll イベント を見よ。

IDL 定義
interface SVGEvent : events::Event {};


インターフェース SVGZoomEvent

SVG ズームイベントセットが DOM 実装により実装されたかどうかを判定するには、 DOMImplementation インターフェースの hasFeature を利用することができる。 このイベントセットに対する特色機能文字列は "SVGZoomEvents" である。この文字列は createEvent メソッドにも利用される。

zoom イベントハンドラはズームイベントが処理される前に呼び出され、 DOM における他の部分は文書の以前の状態のままである。文書はイベントハンドラが正常に終了したときに更新される。

以下のものがズームイベントに対するUIイベント型である:

SVGZoom
ズームイベント利用者により SVG 文書片の現在の表示が等倍されたときに生ずる。イベントハンドラは 'svg' 要素でのみ有効である。 SVGZoom イベント を見よ。

IDL 定義
interface SVGZoomEvent : events::UIEvent { 
  readonly attribute SVGRect zoomRectScreen;
  readonly attribute float previousScale;
  readonly attribute SVGPoint previousTranslate;
  readonly attribute float newScale;
  readonly attribute SVGPoint newTranslate;
};

属性
readonly SVGRect zoomRectScreen

スクリーン単位 (訳注: screen unit - ディスプレイなどの物理的装置における物理的画素に対応する単位と思われる。) で指定されるズーム矩形。

オブジェクト自身とその内容は共に読み出し専用である。

readonly float previousScale
ズーム操作が行われる前の前回のズーム操作後における倍率。
readonly SVGPoint previousTranslate

ズーム操作が行われる前の前回のズーム操作後における並進量

オブジェクト自身とその内容は共に読み出し専用である。

readonly float newScale
ズーム操作が行われた後の倍率
readonly SVGPoint newTranslate

ズーム操作が行われた後の並進量。

オブジェクト自身とその内容は共に読み出し専用である。