SVG中的文本不会出现(Text in SVG does not appear)
我试图用javascript将文本添加到svg元素。
这是我的JavaScript代码:
var svg=document.getElementById("svgtext"); var newText = document.createElementNS(svg,"text"); newText.setAttributeNS(null,"x",0); newText.setAttributeNS(null,"y",50); newText.setAttributeNS(null,"font-size","100"); newText.setAttributeNS(null,"fill","black"); var textNode = document.createTextNode("Hello World"); newText.appendChild(textNode); document.getElementById("gText").appendChild(newText);
这是HTML
<svg id="svgtext" width=160px height=250px> <g id="gText"> <text x="0" y="15" fill="black">SVG!</text> </g> </svg>
正如你可以看到,如果我手动添加一些文本,它完美的作品。 当我尝试使用JavaScript时,我可以看到它出现在Chrome的检查器工具中,但它不会显示在屏幕上。
I`m trying to add text to a svg element with javascript.
Here is my javascript code:
var svg=document.getElementById("svgtext"); var newText = document.createElementNS(svg,"text"); newText.setAttributeNS(null,"x",0); newText.setAttributeNS(null,"y",50); newText.setAttributeNS(null,"font-size","100"); newText.setAttributeNS(null,"fill","black"); var textNode = document.createTextNode("Hello World"); newText.appendChild(textNode); document.getElementById("gText").appendChild(newText);
and this is the HTML
<svg id="svgtext" width=160px height=250px> <g id="gText"> <text x="0" y="15" fill="black">SVG!</text> </g> </svg>
As you can see if I add manually some text, it works perfectly. When I try with javascript I can see it present in Chrome's inspector tool, but it just does not appear on the screen.
原文:https://stackoverflow.com/questions/42189786
最满意答案
我使用gdb来调试你的代码,我发现
buffer->factors
的大小是8,这还不够。你应该做
buffer->factors = malloc(buffer->size*sizeof(int *));
此
segmentation fault
错误可以修复。此外,您的代码中还有另一个错误。 如果
consumer
首先执行,则输出将为空,因此您需要使用cond
首先使producer
exectue。这里有一些提示:
pthread_cond_t product = PTHREAD_COND_INITIALIZER; /*global*/ pthread_cond_signal(&product); /*In factor function*/
此外,
displayFactors
需要添加一个互斥锁 。/*in displayFactors function*/ pthread_cond_wait(&product, &mutex1);
在哪里发表这些陈述,你应该自己解决。 希望能帮到你。
I use gdb to debug your code, I found that the size of
buffer->factors
is 8, that is not enough.you should do
buffer->factors = malloc(buffer->size*sizeof(int *));
This
segmentation fault
error can fix.Also,There is another bug in your code. if the
consumer
execute first, the output will empty, so you need use thecond
to make theproducer
exectue first.Here some tips:
pthread_cond_t product = PTHREAD_COND_INITIALIZER; /*global*/ pthread_cond_signal(&product); /*In factor function*/
Also the
displayFactors
need to add a mutex./*in displayFactors function*/ pthread_cond_wait(&product, &mutex1);
And where to put those statements, you should figure it out by yourself. Hope that can help you.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
错误在这里: memmove(posb,posa,size1*size2*sizeof(int)); 因为您将指针传递给指向整数的指针,但您应该将指针传递给整数: memmove(*posb, *posa, size1 * size2 * sizeof(int)); Free_array_x2方法中也存在错误,因为您没有释放使用malloc从系统中获取的所有内存 void Free_array_x2(int **ptr){ free(ptr); ptr=NULL; } 这应该转到你在分 ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
C ++使您可以控制要分配内存的位置。 在您的情况下,您发现的是您在堆栈上分配了一个超出堆栈大小的array-of-int。 在某些时候,您可以访问位于堆栈边界之外的这些元素之一以及程序,这会导致称为分段错误的访问冲突。 既然你提到了C ++的新手,那将有助于理解这3个内存区域以及如何将它们用于你的案例: 堆栈内存 - 临时变量自动使用的空间,无需显式请求。 如果超出堆栈大小,您将看到未定义的行为。 int main() { int arr[100000][100000]; } 堆内存 - 用于在使用 ...
-
2D阵列的分段故障(segmentation fault for 2D arrays)[2023-05-23]
您可以只在一个数组中分配整个表,但是您将无法使用两个方括号访问索引的数组数据: int * temp = malloc(4000*5000*sizeof(int)); 要访问之前你写过temp[i][j]的元素(i,j),现在你应该按照以下方式计算索引: temp[i*5000+j]; 并且不要忘记随后释放为你的桌子分配的内存: free(temp); You can allocate the whole table in only one array but you won't be able to ... -
将内存分配给3维char数组会导致分段错误(Allocating memory to a 3-dimensional char array causes segmentation fault)[2022-07-31]
你错过了一些大括号: for(i = 0; i < sections; i++) { // ... } You're missing some braces: for(i = 0; i < sections; i++) { // ... } -
我使用gdb来调试你的代码,我发现buffer->factors的大小是8,这还不够。 你应该做 buffer->factors = malloc(buffer->size*sizeof(int *)); 此segmentation fault错误可以修复。 此外,您的代码中还有另一个错误。 如果consumer首先执行,则输出将为空,因此您需要使用cond首先使producer exectue。 这里有一些提示: pthread_cond_t product = PTHREAD_COND_INITIAL ...
-
分配2d阵列(Allocating a 2d Array)[2021-10-21]
我认真不建议这样做,因为您的代码中有大量关于高端大小的假设。 但是如果你真的想要这样做,那么在C中返回指向固定长度数组的指针的相当神秘的语法看起来像这样: #include#include #define NUMOFCOL 4 int (*addtwoarr(int A[][NUMOFCOL], int B[][NUMOFCOL]))[NUMOFCOL]; int main(void) { int firstarr[][NUMOFCOL] = { ... -
问题是你堆满了。 当你调用initialize_mx()它为它的局部变量(在你的情况下为confmx initialize_mx()分配堆栈空间。 如果局部变量太大,这个空间受操作系统的限制(如果你在Linux上,请检查ulimit)。 基本上你可以: 正如cnicutar所暗示的那样,将confmx声明为全局变量。 动态分配数组的内存空间。 并传递一个指向initialize_mx()的指针 编辑:刚刚意识到你必须仍然分配内存空间,如果你传递一个指针,所以你有这两个选项:) The problem is ...
-
在这一行: lines[outerIndex] = (char*) realloc(lines[outerIndex], (widths+1)*sizeof(char)); 你提供一个未初始化的指针作为realloc的第一个参数。 你应该在这里使用malloc 。 其他事宜: char c应该是int c (阅读getchar的文档以了解原因)。 如果输入以空格开头,则lines[outerIndex][innerIndex]='\0'写出界限。 代码块起始if(innerIndex==widths)在代 ...