Request

Response

    Add Toolbar Button

    In this tutorial we’ll be adding a custom toolbar with two buttons to The Viewer canvas. Each button will have its own tooltip and will react to click events. Click events will trigger camera rotation of 90 degrees around the target.

    ../../../_images/custom_toolbar3.jpg

    You will also listen and react to events dispatched by the Viewer instance.

    Before You Begin

    Instantiate a ViewingApplication before moving ahead; it provides a solid starting point.

    We recommend the code in this tutorial to be encapsulated in a developer-created extension. This tutorial will assume you already have a custom extension created which is being loaded by your Viewer application. Don’t forget to assign a unique extension id to this new extension!

    Step 1: Detect the Toolbar

    Depending on when the extension gets loaded, the toolbar object might not be available. When no toolbar is present, you’ll have to wait for TOOLBAR_CREATED_EVENT to get fired. See the Events tutorial for more details.

    We will use the extension’s load() method to encapsulate this logic.

    ToolbarExtension.prototype.load = function() {
    
      if (this.viewer.toolbar) {
        // Toolbar is already available, create the UI
        this.createUI();
      } else {
        // Toolbar hasn't been created yet, wait until we get notification of its creation
        this.onToolbarCreatedBinded = this.onToolbarCreated.bind(this);
        this.viewer.addEventListener(av.TOOLBAR_CREATED_EVENT, this.onToolbarCreatedBinded);
      }
    
      return true;
    };
    
    ToolbarExtension.prototype.onToolbarCreated = function() {
      this.viewer.removeEventListener(av.TOOLBAR_CREATED_EVENT, this.onToolbarCreatedBinded);
      this.onToolbarCreatedBinded = null;
      this.createUI();
    };
    
    ToolbarExtension.prototype.createUI = function() {
      alert('TODO: Create Toolbar!');
    };
    
    Show More

    The code above makes use of JavaScript’s native bind() method to make sure accessing this is locked to the ToolbarExtension object.

    Step 2: Add Buttons

    Method createUI() will get called whenever a toolbar is available for manipulation. Let’s now create a sub-toolbar and add a couple buttons.

    ToolbarExtension.prototype.createUI = function() {
      // alert('TODO: Create Toolbar!');
    
      var viewer = this.viewer;
    
      // Button 1
      var button1 = new Autodesk.Viewing.UI.Button('my-view-front-button');
      button1.onClick = function(e) {
          viewer.setViewCube('front');
      };
      button1.addClass('my-view-front-button');
      button1.setToolTip('View front');
    
      // Button 2
      var button2 = new Autodesk.Viewing.UI.Button('my-view-back-button');
      button2.onClick = function(e) {
          viewer.setViewCube('back');
      };
      button2.addClass('my-view-back-button');
      button2.setToolTip('View Back');
    
      // SubToolbar
      this.subToolbar = new Autodesk.Viewing.UI.ControlGroup('my-custom-view-toolbar');
      this.subToolbar.addControl(button1);
      this.subToolbar.addControl(button2);
    
      viewer.toolbar.addControl(this.subToolbar);
    };
    
    Show More

    You will notice that the code above calls method addClass(), which adds a CSS class to specialize the look of our custom buttons. We would now need to define these classes. For this tutorial, we’ll add these definitions in our HTML:

    <style>
      .my-view-front-button {
        background: red;
      }
      .my-view-back-button {
        background: blue;
      }
    </style>
    
    Show More

    Refresh the HTML page and notice your custom buttons onscreen. Hovering over them will trigger their tooltips. Click to trigger their actions.

    Step 3: Cleanup

    Extensions should remove any DOM elements and events they add. In this case, we need only to remove this.subToolbar.

    ToolbarExtension.prototype.unload = function() {
      this.viewer.toolbar.removeControl(this.subToolbar);
      return true;
    };
    

    As with the extensions tutorial, you can verify that the extension works as expected by manually calling load() and unload() methods.

     
    Version 5
    ______
    icon-svg-close-thick

    Cookie preferences

    Your privacy is important to us and so is an optimal experience. To help us customize information and build applications, we collect data about your use of this site.

    May we collect and use your data?

    Learn more about the Third Party Services we use and our Privacy Statement.

    Strictly necessary – required for our site to work and to provide services to you

    These cookies allow us to record your preferences or login information, respond to your requests or fulfill items in your shopping cart.

    Improve your experience – allows us to show you what is relevant to you

    These cookies enable us to provide enhanced functionality and personalization. They may be set by us or by third party providers whose services we use to deliver information and experiences tailored to you. If you do not allow these cookies, some or all of these services may not be available for you.

    Customize your advertising – permits us to offer targeted advertising to you

    These cookies collect data about you based on your activities and interests in order to show you relevant ads and to track effectiveness. By collecting this data, the ads you see will be more tailored to your interests. If you do not allow these cookies, you will experience less targeted advertising.

    icon-svg-close-thick

    THIRD PARTY SERVICES

    Learn more about the Third-Party Services we use in each category, and how we use the data we collect from you online.

    icon-svg-hide-thick

    icon-svg-show-thick

    Strictly necessary – required for our site to work and to provide services to you

    Qualtrics
    W
    Akamai mPulse
    W
    Digital River
    W
    Dynatrace
    W
    Khoros
    W
    Launch Darkly
    W
    New Relic
    W
    Salesforce Live Agent
    W
    Wistia
    W
    Tealium
    W
    Upsellit
    W
    CJ Affiliates
    W
    Commission Factory
    W
    Google Analytics (Strictly Necessary)
    W
    Typepad Stats
    W
    Geo Targetly
    W
    SpeedCurve
    W
    Qualified
    #

    icon-svg-hide-thick

    icon-svg-show-thick

    Improve your experience – allows us to show you what is relevant to you

    Google Optimize
    W
    ClickTale
    W
    OneSignal
    W
    Optimizely
    W
    Amplitude
    W
    Snowplow
    W
    UserVoice
    W
    Clearbit
    #
    YouTube
    #

    icon-svg-hide-thick

    icon-svg-show-thick

    Customize your advertising – permits us to offer targeted advertising to you

    Adobe Analytics
    W
    Google Analytics (Web Analytics)
    W
    AdWords
    W
    Marketo
    W
    Doubleclick
    W
    HubSpot
    W
    Twitter
    W
    Facebook
    W
    LinkedIn
    W
    Yahoo! Japan
    W
    Naver
    W
    Quantcast
    W
    Call Tracking
    W
    Wunderkind
    W
    ADC Media
    W
    AgrantSEM
    W
    Bidtellect
    W
    Bing
    W
    G2Crowd
    W
    NMPI Display
    W
    VK
    W
    Adobe Target
    W
    Google Analytics (Advertising)
    W
    Trendkite
    W
    Hotjar
    W
    6 Sense
    W
    Terminus
    W
    StackAdapt
    W
    The Trade Desk
    W
    RollWorks
    W

    Are you sure you want a less customized experience?

    We can access your data only if you select "yes" for the categories on the previous screen. This lets us tailor our marketing so that it's more relevant for you. You can change your settings at any time by visiting our privacy statement

    Your experience. Your choice.

    We care about your privacy. The data we collect helps us understand how you use our products, what information you might be interested in, and what we can improve to make your engagement with Autodesk more rewarding.

    May we collect and use your data to tailor your experience?

    Explore the benefits of a customized experience by managing your privacy settings for this site or visit our Privacy Statement to learn more about your options.