What is paper and set in Raphael.js
Is it some external library reference..?
What is its use and how to use it..?
A Paper is a Raphael reference to its main SVG element that it uses, a bit like a container (you can have several). It also has extra methods and variables, so its not 'just' an SVG element, but you can sort of think of it a bit like the main SVG element.
A Set is like an Array, thats used to store Raphael elements.
When its useful, is iterating over a large amount of Raph elements.
So you may do something like.
var mySet = paper.set();
mySet.push( myCircle, myRect, myOtherShapeCreatedEarlier);
mySet.forEach( function( el ) { doSomethingWithEachElement() } );
Also you may do something like...
var mySet = paper.selectAll('path');
mySet.attr({ opacity: "0" });
Which would make all the paths vanish.
So really a set is just a way of dealing with elements in an easy way.
Related
I have two SVG files: intial.svg and final.svg. I want to morph initial.svg onto final.svg on button click event. I have gone through the libraries suggested in this question but there is no clear documentation or example on how to achieve this specific morph. I have exported these animations from an XD prototype. I want to achieve a simple ease-in animation by specifying the initial state of an svg and the final state of the same svg. Any recommendations would be highly appreciated.
If the SVGs are (or can be) drawn from the same paths, then I would suggest the NPM library svg-path-morph. It allows you to interpolate freely between an arbitrary number of SVG paths.
An example of its usage:
import { compile, morph } from 'svg-path-morph'
// Get the d attributes of the <path> elements you want to morph between
const happy = document.getElemenyById('happy').getAttribute('d')
const angry = document.getElemenyById('angry').getAttribute('d')
// Compile the morph base (average path embedding)
const compiled = compile([
happy,
angry
])
// Morph between the happy/angry faces
const slightlyAngry = morph(
compiled,
[
0.80, // 80% happy
0.20 // 20% angry
]
)
// Use the face is the d attribute of a <path> element
document.getElementById('the-face').setAttribute('d', slightlyAngry)
really new to programming, python and OOP.
In Python3 and PyQt5, I have a number of objects: QLineEdit(s) defined in Designer and loaded
with uicload, I use to get inputs, I found a way to validate them, my list looks like:
self.validatorint = QtGui.QIntValidator()
self.inputguiwin.annualsalaryinput.setValidator(self.validatorint)
self.inputguiwin.annualsalaryinput.textChanged.connect(self.check_state)
self.inputguiwin.annualsalaryinput.textChanged.emit(self.inputguiwin.annualsalaryinput.text())
self.inputguiwin.annualsalaryinput.textChanged.connect(self.disablepushButtonOK)
self.validatordouble = QtGui.QDoubleValidator(0.100, 1.00, 2)
self.inputguiwin.tosaveinput.setValidator(self.validatordouble)
self.inputguiwin.tosaveinput.textChanged.connect(self.check_state)
self.inputguiwin.tosaveinput.textChanged.emit(self.inputguiwin.houseinput.text())
self.inputguiwin.tosaveinput.textChanged.connect(self.disablepushButtonOK)
self.validatorint = QtGui.QIntValidator()
self.inputguiwin.houseinput.setValidator(self.validatorint)
self.inputguiwin.houseinput.textChanged.connect(self.check_state)
self.inputguiwin.houseinput.textChanged.emit(self.inputguiwin.houseinput.text())
self.inputguiwin.houseinput.textChanged.connect(self.disablepushButtonOK)
self.inputguiwin.pushButtonOK.setEnabled(False)
self.inputguiwin.annualsalaryinput is a QLineEdit Widget in may main GUI inputguiwin,
then I have tosaveinput and houseinput. The lines above connect the inputs to a function
that changes the background color of my input when it is validated and re-able my OK PushButton
Thinking about extending the number of inputs I was wondering about a way of iterating the
four lines of code for all my QLinesEdit widgets.
Now, I dont understand a lot about Python, classes, object and so on. Which is the best or a way to iterate over the 'list' of my widgets ? I mean is there a way to have a list of objects in Python,
what is the best way to iterate over the widgets in my example? at least the ones that will share the same identical QtGui.QIntValidator() type ?
If you want to reduce your code and make it more readable then you will have to use lists and iterate over them:
for lineedit, validator in (
(self.inputguiwin.annualsalaryinput, QtGui.QIntValidator(),),
(self.inputguiwin.tosaveinput, QtGui.QDoubleValidator(0.100, 1.00, 2),),
(self.inputguiwin.houseinput, QtGui.QIntValidator(),),
):
lineedit.setValidator(validator)
lineedit.textChanged.connect(self.check_state)
lineedit.textChanged.emit(lineedit.text())
lineedit.textChanged.connect(self.disablepushButtonOK)
thanks again I ended up with:
input_list_int=[self.inputguiwin.annualsalaryinput, self.inputguiwin.houseinput]
for i in input_list_int:
i_inputguiwin=i
self.validatorint = QtGui.QIntValidator()
i_inputguiwin.setValidator(self.validatorint)
i_inputguiwin.textChanged.connect(self.check_state)
i_inputguiwin.textChanged.emit(i_inputguiwin.text())
i_inputguiwin.textChanged.connect(self.disablepushButtonOK)
input_list_double=[self.inputguiwin.tosaveinput]
for i in input_list_double:
i_inputguiwin=i
self.validatordouble = QtGui.QDoubleValidator(0.100, 1.00, 2)
i_inputguiwin.setValidator(self.validatordouble)
i_inputguiwin.textChanged.connect(self.check_state)
i_inputguiwin.textChanged.emit(i_inputguiwin.text())
i_inputguiwin.textChanged.connect(self.disablepushButtonOK)
obviously your method is better, I'll try to dig more into lists
How can I get the label of an equation? I'm attempting to reprocess an equation with a label, but I have to delete the label from MathJax.Extension["TeX/AMSmath"].labels first, for which the label must be known...
I know I can scan through the source text for the label MathJax.Hub.getAllJax("mathDiv")[0}.SourceElement().find("\label(") (...), but this seems needlessly complicated. Is there a better way?
There's no built-in API for this.
If you don't need to keep labels, then the reset in the comment above is probably the best way to go about it:
MathJax.Extension["TeX/AMSmath"].labels = {}
A quick and dirty way to get the IDs is to leverage the fact that they end up in the output. So you can just get all the IDs in the output, e.g.,
const math = MathJax.Hub.getAllJax()[0];
const nodesWithIds = document.getElementById(math.root.inputID).previousSibling.querySelectorAll('[id]');
const ids = [];
for (node of nodesWithIds) ids.push(node.id);
A cleaner and perhaps conceptually easier way would be to leverage MathML (which is essentially the internal format): the \label{} always ends up on an mlabeledtr. The trouble is that you'd have to re-parse that, e.g.,
const temp = document.createElement('span');
temp.innerHTML = math.root.toMathML();
const nodesWithIds = temp.querySelectorAll('mlabeledtr [id]');
const ids = [];
for (node of nodesWithIds) ids.push(node.id);
This will make sure the array only has relevant IDs in them (and the contents of the nodes should correspond to \label{}.
I suppose with helper libraries it might be easier to dive into the math.root object directly and look for IDs recursively (in its data key).
I have a canvas.
In this canvas I have a closed path, and I am trying to morph this path into a different path.
Paths can have any number of points(>=3).
I have two paths:
var path1 = "M50,50L200,50,200,200,50,200z"
var path2 = "M300,200L50,200,50,50,200,50z"
This is what I'm using to animate the morphing:
var path = paper.path(path1).attr({'stroke':'black','fill':'white'})
var currentPath = 1
path.click(function () {
if(currentPath==1) {
path.stop().animate({d: path2},2000, function () {
currentPath=2
})
}
else {
path.stop().animate({d: path1},2000,function () {
currentPath=1
})
}
})
This is the situation I want to achieve:
http://jsfiddle.net/MichaelSel/vgw3qxpg/7/
This is the situation I want to avoid.
http://jsfiddle.net/MichaelSel/vgw3qxpg/6/
Is there any way to tell snap to do the animation by using the shortest distance to each point?
Note: I cannot just 'rewrite' the paths in reverse order (which would fix them) because it's the client who positions the points arbitrarily.
What can I do?
I would love to add more details if my question is unclear.
Thank you all.
No, there is no way to do this (other than coding your own complex morphing with checks).
It uses basic interpolation between the points, so its important to get the devs to move the points in their svg creation rather than just creating a new svg from scratch that could have points start from any position.
There is no reason though to my knowledge why you couldn't still rotate the path points with a bit of clever code though, even if the client has positioned them arbitrarily, but I don't think thats simple.
I have several groups (SVG G elements) nested in another group and I would like to get their ID's. I used the D3 javascript library to create the SVG and the code looks similar to this.
var body = d3.select("body");
var svg = body.append("svg")
.attr("width", '100%')
.attr("height", '100%')
var outerG = svg.append("g")
.attr('id','outerG')
var innerG1 = outerG.append('g')
.attr('id','innerG1')
var innerG2 = outerG.append('g')
.attr('id','innerG2')
I tried to use childNodes attribute, but console.log(outerG[0].childNodes) gives me undefined. Could not find the right answer searching with google, could please someone give me a hint how to do that ?
This will work:
console.log(outerG[0][0].childNodes);
See jsFiddle here
The reason you need two nested indices is that all selections are grouped implicitly. If you'd like to know the deeper reasons for this, or want to understand selections better in general, see this article