Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
triangle.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_webcanv
3/// User class with custom JavaScript painter in the web canvas.
4///
5/// Custom class is just triangle which drawn on the frame with NDC coordinates
6/// `triangle.mjs` provides JavaScript code for object painting and interactivity
7/// It is also possible to use such "simple" class without loading of custom JS code,
8/// but then it requires appropriate Paint() method and will miss interactivity in browser
9///
10/// This macro must be executed with ACLiC like 'root --web triangle.cxx+'
11///
12/// \macro_image (tcanvas_jsp)
13/// \macro_code
14///
15/// \author Sergey Linev
16
17#include <iostream>
18
19#include "TNamed.h"
20#include "TAttLine.h"
21#include "TAttFill.h"
22#include "TWebCanvas.h"
23#include "TCanvas.h"
24#include "TROOT.h"
25
27
28class TTriangle : public TNamed, public TAttLine, public TAttFill {
29 double fX[3] = { 0, 0, 0 };
30 double fY[3] = { 0, 0, 0 };
31
32 public:
33 TTriangle() {} // =default not allowed !!!
34
35 TTriangle(const char *name, const char *title = "") : TNamed(name, title) {}
36
37 void SetPoints(double x1, double y1, double x2, double y2, double x3, double y3)
38 {
39 fX[0] = x1;
40 fY[0] = y1;
41 fX[1] = x2;
42 fY[1] = y2;
43 fX[2] = x3;
44 fY[2] = y3;
45 }
46
47 /** In old graphics just provide line drawing */
48 void Paint(Option_t *opt) override
49 {
50 if (!gPad) return;
51 TAttLine::Modify(); //Change line attributes only if necessary
52 TAttFill::Modify(); //Change fill area attributes only if necessary
53
54 if (*opt == 'f')
55 gPad->PaintFillAreaNDC(3, fX, fY, opt);
56 else
57 gPad->PaintPolyLineNDC(3, fX, fY, opt);
58 }
59
60 ClassDefOverride(TTriangle, 1); // Example of triangle drawing in web canvas
61};
62
63void triangle(bool ignore_jsmodule = false)
64{
65 if (ignore_jsmodule) {
66 printf("Custom JS module will NOT be provided for TTriangle class\n");
67 printf("No interactive features will be available\n");
68 printf("TTriangle::Paint() method will be used for object painting - also in web case\n");
69 } else {
70 #ifdef __CLING__
71 printf("Please run this script in compiled mode by running \".x triangle.cxx+\"\n");
72 printf("Requires to properly generate dictionary for TTriangle class\n");
73 return;
74 #endif
75
76 std::string fdir = __FILE__;
77 auto pos = fdir.find("triangle.cxx");
78 if (pos != std::string::npos)
79 fdir.resize(pos);
80 else
81 fdir = gROOT->GetTutorialsDir() + std::string("/webcanv/");
82
83 // location required to load files
84 // also it is name of modules path used in importmap
85 ROOT::RWebWindowsManager::AddServerLocation("tutorials_webcanv", fdir);
86
87 // mark TTriangle as supported on the client
88 TWebCanvas::AddCustomClass("TTriangle");
89
90 // specify which extra module should be loaded,
91 // "tutorials_webcanv/" is registered path from server locations
92 TWebCanvas::SetCustomScripts("modules:tutorials_webcanv/triangle.mjs");
93 }
94
95 auto tr1 = new TTriangle("tr1", "first triangle");
96 tr1->SetPoints(0.4, 0.5, 0.8, 0.3, 0.8, 0.7);
97 tr1->SetLineColor(kRed);
98 tr1->SetLineWidth(3);
99 tr1->SetFillColor(kBlue);
100
101 auto tr2 = new TTriangle("tr2", "second triangle");
102 tr2->SetPoints(0.2, 0.2, 0.2, 0.6, 0.5, 0.4);
103 tr2->SetLineColor(kGreen);
104 tr2->SetLineStyle(kDashed);
105 tr2->SetFillColor(kYellow);
106
107 auto tr3 = new TTriangle("tr3", "third triangle");
108 tr3->SetPoints(0.4, 0.8, 0.6, 0.8, 0.5, 0.2);
109 tr3->SetLineColor(kCyan);
110 tr3->SetFillColor(kMagenta);
111
112 auto c1 = new TCanvas("c1", "Triangles");
113 c1->Add(tr1, "f");
114 c1->Add(tr2, "f");
115 c1->Add(tr3, "f");
116
117 // test image saving with web browser, chrome or firefox are required
118 // c1->SaveAs("triangle.png");
119 // c1->SaveAs("triangle.pdf");
120}
const char Option_t
Definition RtypesCore.h:66
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kMagenta
Definition Rtypes.h:66
@ kCyan
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
@ kYellow
Definition Rtypes.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
@ kDashed
Definition TAttLine.h:48
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
#define gPad
static void AddServerLocation(const std::string &server_prefix, const std::string &files_path)
Configure server location which can be used for loading of custom scripts or files When THttpServer i...
Fill Area Attributes class.
Definition TAttFill.h:19
virtual void Modify()
Change current fill area attributes if necessary.
Definition TAttFill.cxx:216
Line Attributes class.
Definition TAttLine.h:18
virtual void Modify()
Change current line attributes if necessary.
Definition TAttLine.cxx:247
The Canvas class.
Definition TCanvas.h:23
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Definition TObject.cxx:624
static void AddCustomClass(const std::string &clname, bool with_derived=false)
Assign custom class.
static void SetCustomScripts(const std::string &src)
Configures custom script for canvas.
return c1
Definition legend1.C:41