Inheritance: SharpKit.Html.HtmlContext, IScoreRenderer
Exemple #1
0
 protected override IScoreRenderer CreateScoreRenderer(Settings settings, object rawSettings, HtmlElement canvasElement)
 {
     var renderer = new WorkerScoreRenderer(this, rawSettings);
     renderer.PostRenderFinished += () =>
     {
         Element.className = Element.className.replace(" loading", "")
                                     .replace(" rendering", "");
     };
     return renderer;
 }
        protected override IScoreRenderer CreateScoreRenderer(Settings settings, object rawSettings, HtmlElement canvasElement)
        {
            var renderer = new WorkerScoreRenderer(this, rawSettings);

            renderer.PostRenderFinished += () =>
            {
                Element.className = Element.className.replace(" loading", "")
                                    .replace(" rendering", "");
            };
            return(renderer);
        }
Exemple #3
0
        protected override IScoreRenderer CreateScoreRenderer(Settings settings)
        {
            var renderer = new WorkerScoreRenderer(settings);

            renderer.ScoreLoaded += score =>
            {
                ScoreLoaded(score, false);
            };
            renderer.PostRenderFinished += () =>
            {
                Element.className = Element.className.replace(" loading", "")
                                    .replace(" rendering", "");
            };
            return(renderer);
        }
Exemple #4
0
 protected override IScoreRenderer CreateScoreRenderer(Settings settings)
 {
     var renderer = new WorkerScoreRenderer(settings);
     renderer.ScoreLoaded += score =>
     {
         ScoreLoaded(score, false);
     };
     renderer.PostRenderFinished += () =>
     {
         Element.className = Element.className.replace(" loading", "")
                                     .replace(" rendering", "");
     };
     return renderer;
 }
Exemple #5
0
        protected JsApi(HtmlElement element, dynamic options)
        {
            Element = element;

            Element.classList.add("alphaTab");

            // load settings
            var dataAttributes = GetDataAttributes();
            var settings       = Settings = AlphaTab.Settings.FromJson(options, dataAttributes);
            var autoSize       = settings.Width < 0;

            #region build tracks array

            // get track data to parse
            dynamic tracksData;
            if (options != null && options.tracks)
            {
                tracksData = options.tracks;
            }
            else
            {
                if (dataAttributes.ContainsKey("tracks"))
                {
                    tracksData = dataAttributes["tracks"];
                }
                else
                {
                    tracksData = 0;
                }
            }

            SetTracks(tracksData, false);

            #endregion

            string contents = "";
            if (element != null)
            {
                // get load contents

                if (dataAttributes.ContainsKey("tex") && element.innerText.As <JsBoolean>())
                {
                    contents          = (element.innerHTML.As <string>()).Trim();
                    element.innerHTML = "";
                }

                #region Create context elements (wrapper, canvas etc)

                CanvasElement = (HtmlElement)document.createElement("div");

                CanvasElement.className        = "alphaTabSurface";
                CanvasElement.style.fontSize   = "0";
                CanvasElement.style.overflow   = "hidden";
                CanvasElement.style.lineHeight = "0";
                element.appendChild(CanvasElement);

                #endregion

                #region Setup scroll and resize handlers for lazy-loading

                if (settings.Engine == "default" || settings.Engine == "svg")
                {
                    window.addEventListener("scroll", e =>
                    {
                        ShowSvgsInViewPort();
                    }, true);
                    window.addEventListener("resize", e =>
                    {
                        ShowSvgsInViewPort();
                    }, true);
                }

                #endregion

                #region Auto Sizing

                if (autoSize)
                {
                    settings.Width = element.offsetWidth;
                    int timeoutId = 0;
                    window.addEventListener("resize", e =>
                    {
                        clearTimeout(timeoutId);
                        timeoutId = setTimeout(() =>
                        {
                            if (element.offsetWidth != settings.Width)
                            {
                                TriggerResize();
                            }
                        }, 1);
                    });
                }

                #endregion
            }

            #region Renderer Setup

            CreateStyleElement(settings);

            if (element != null && autoSize)
            {
                var initialResizeEventInfo = new ResizeEventArgs();
                initialResizeEventInfo.OldWidth = 0;
                initialResizeEventInfo.NewWidth = element.offsetWidth;
                initialResizeEventInfo.Settings = settings;
                TriggerEvent("resize", initialResizeEventInfo);
                settings.Width = initialResizeEventInfo.NewWidth;
            }

            var workersUnsupported = !window.Worker.As <bool>();
            if (settings.UseWebWorker && !workersUnsupported && settings.Engine != "html5")
            {
                Renderer = new WorkerScoreRenderer(this, settings);
            }
            else
            {
                Renderer = new ScoreRenderer(settings);
            }
            Renderer.RenderFinished     += o => TriggerEvent("rendered");
            Renderer.PostRenderFinished += () =>
            {
                Element.classList.remove("loading");
                Element.classList.remove("rendering");
                TriggerEvent("postRendered");
            };
            Renderer.PreRender += result =>
            {
                _renderResults    = new FastList <RenderFinishedEventArgs>();
                _totalResultCount = 0;
                AppendRenderResult(result);
            };
            Renderer.PartialRenderFinished += AppendRenderResult;
            Renderer.RenderFinished        += r =>
            {
                AppendRenderResult(r);
                AppendRenderResult(null); // marks last element
            };
            Renderer.Error += Error;

            #endregion

            #region Load Default Data

            Action initialRender = () =>
            {
                // rendering was possibly delayed due to invisible element
                // in this case we need the correct width for autosize
                if (autoSize)
                {
                    Settings.Width = Element.offsetWidth;
                    Renderer.UpdateSettings(settings);
                }

                if (!string.IsNullOrEmpty(contents))
                {
                    Tex(contents);
                }
                else if (options && options.file)
                {
                    Load(options.file);
                }
                else if (dataAttributes.ContainsKey("file"))
                {
                    Load(dataAttributes["file"]);
                }
            };

            _visibilityCheckerInterval = options && options.visibilityCheckInterval || 500;
            if (IsElementVisible)
            {
                // element is visible, so we start rendering
                initialRender();
            }
            else
            {
                // if the alphaTab element is not visible, we postpone the rendering
                // we check in a regular interval whether it became available.
                Logger.Warning("Rendering", "AlphaTab container is invisible, checking for element visibility in " + _visibilityCheckerInterval + "ms intervals");
                _visibilityCheckerIntervalId = setInterval(() =>
                {
                    if (IsElementVisible)
                    {
                        Logger.Info("Rendering", "AlphaTab container became visible, triggering initial rendering");
                        initialRender();
                        clearInterval(_visibilityCheckerIntervalId);
                        _visibilityCheckerIntervalId = 0;
                    }
                }, _visibilityCheckerInterval);
            }

            #endregion
        }